GriefNorth

Untitled

Sep 9th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. from BeautifulSoup import BeautifulSoup as Soup
  2. from datetime import date
  3. from urllib2 import urlopen
  4. import soupselect; soupselect.monkeypatch()
  5.  
  6. def getCurrencyRate():
  7. result = {}
  8. today = date.today()
  9. currencyDict = {'usd': 'R01235', 'eur': 'R01239'}
  10.  
  11. currentDay = today.day
  12. currentMonth = today.month
  13. currentYear = today.year
  14.  
  15. if currentDay > 1:
  16. startDate = date(currentYear, currentMonth, 1)
  17. else:
  18. startDate = date(currentYear, currentMonth-1, 1)
  19. endDate = date(currentYear+1, 1, 1)
  20.  
  21. data = {
  22. 'cbrUrl': 'http://www.cbr.ru/currency_base/DD_print.aspx',
  23. 'startDate': startDate.strftime('%d.%m.%Y'),
  24. 'endDate': endDate
  25. }
  26. uri = u'%(cbrUrl)s?date_req1=%(startDate)s&date_req2=%(endDate)s&VAL_NM_RQ=' % data
  27.  
  28. for i in currencyDict.iteritems():
  29. html = urlopen(uri + i[1])
  30. soup = Soup(html.read())
  31. currencyString = soup.findSelect('table.CBRTBL td')[-1].text
  32. result[i[0]] = float(currencyString.replace(',', '.'))
  33. return result
  34.  
  35. print getCurrencyRate()
Advertisement
Add Comment
Please, Sign In to add comment