Skip to content

Instantly share code, notes, and snippets.

Last active January 20, 2022 03:19
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__':
Copy link

Is it's leagal to fetch data from that URL? , can we use this commercial use?

Copy link

@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