Created
May 1, 2016 15:01
-
-
Save AshikNesin/cd7374f9ac8180af5f1a8419fe5ec7bd to your computer and use it in GitHub Desktop.
USD to INR Currency Exchange Scraping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import re | |
from bs4 import BeautifulSoup | |
from decimal import Decimal | |
def USD_to_INR(USD): | |
''' | |
- Retrieve last updated and convertion from db | |
- If latest update is less than 24 hours then make is_latest True | |
''' | |
is_latest = False | |
exchange_rate = 1 | |
if not is_latest: | |
r = requests.get('http://www.xe.com/currencyconverter/convert/?Amount=1&From=USD&To=INR',headers={'User-agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36'}) | |
soup = BeautifulSoup(r.content,"lxml") | |
price_tag = soup.find("table",{"class":"ucc-result-table"}).find('td',{'class':'rightCol'}) | |
exchange_rate = Decimal(re.findall("\d+\.\d+",price_tag.text)[0]) | |
# TODO: Save the exhange_rate in db. | |
return "{0:.2f}".format(USD * exchange_rate) | |
print USD_to_INR(100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment