Skip to content

Instantly share code, notes, and snippets.

@binary-ibex
Last active January 20, 2022 03:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binary-ibex/67f03c23ad17a3aced0710fe481998b8 to your computer and use it in GitHub Desktop.
Save binary-ibex/67f03c23ad17a3aced0710fe481998b8 to your computer and use it in GitHub Desktop.
greturns-inr-api
from flask import Flask, jsonify
import requests
import re
from bs4 import BeautifulSoup as bs
app = Flask(__name__)
#website url
url = 'https://www.goodreturns.in/currency/world-currencies-vs-indian-rupee-inr.html'
#rate extracting regex
#regex to fetch the currency name
fetch_currency_name = r'<td\swidth="250">[a-bA-Z0-9].*</td>'
#regex to fetch the rate
fetch_rate = r'<span>[0-9]*\.[0-9]*</span>'
#fetch data from the server
def fetch(url):
#fetch the page and parse it using the bs4
page = requests.get(url)
page_txt = page.text
currency = {}
for i,j in zip(re.findall(fetch_currency_name, page_txt), re.findall(fetch_rate, page_txt)):
currency[bs(i, 'html.parser').text] = bs(j, 'html.parser').text
return currency
#main route
@app.route('/', methods=['POST', 'GET'])
def main():
return jsonify(fetch(url))
if __name__=='__main__':
app.run()
@binary-ibex
Copy link
Author

@rajulapatisairam Web scraping is not illegal in itself, but when you are using it for commercial purposes, you have to look at the terms and conditions of the website in question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment