Skip to content

Instantly share code, notes, and snippets.

@binary-ibex
Last active Nov 19, 2020
Embed
What would you like to do?
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment