Skip to content

Instantly share code, notes, and snippets.

Last active Nov 19, 2020
What would you like to do?
from flask import Flask, jsonify
import requests
import re
from bs4 import BeautifulSoup as bs
app = Flask(__name__)
#website url
url = ''
#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__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment