Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from BeautifulSoup import BeautifulSoup as Soup
- from datetime import date
- from urllib2 import urlopen
- import soupselect; soupselect.monkeypatch()
- def getCurrencyRate():
- result = {}
- today = date.today()
- currencyDict = {'usd': 'R01235', 'eur': 'R01239'}
- currentDay = today.day
- currentMonth = today.month
- currentYear = today.year
- if currentDay > 1:
- startDate = date(currentYear, currentMonth, 1)
- else:
- startDate = date(currentYear, currentMonth-1, 1)
- endDate = date(currentYear+1, 1, 1)
- data = {
- 'cbrUrl': 'http://www.cbr.ru/currency_base/DD_print.aspx',
- 'startDate': startDate.strftime('%d.%m.%Y'),
- 'endDate': endDate
- }
- uri = u'%(cbrUrl)s?date_req1=%(startDate)s&date_req2=%(endDate)s&VAL_NM_RQ=' % data
- for i in currencyDict.iteritems():
- html = urlopen(uri + i[1])
- soup = Soup(html.read())
- currencyString = soup.findSelect('table.CBRTBL td')[-1].text
- result[i[0]] = float(currencyString.replace(',', '.'))
- return result
- print getCurrencyRate()
Advertisement
Add Comment
Please, Sign In to add comment