Flask
Flask
Solution :-
1. Defining a list of companies and their URLs in a configuration file. You can use
the configparser module to read the configuration file. Here's an example of what
your configuration file might look like:
[companies]
IBM = https://finance.yahoo.com/quote/IBM/history?p=IBM
AAPL = https://finance.yahoo.com/quote/AAPL/history?p=AAPL
MSFT = https://finance.yahoo.com/quote/MSFT/history?p=MSFT
GOOG = https://finance.yahoo.com/quote/GOOG/history?p=GOOG
AMZN = https://finance.yahoo.com/quote/AMZN/history?p=AMZN
2. Here writing a Python code that will read the configuration file and scrapes
the finance data for each company. For each company, you can extract the data using
BeautifulSoup and store it in a pandas DataFrame. Here's an example of how you can
scrape the data for IBM:
import requests
from bs4 import BeautifulSoup
import pandas as pd
import configparser
import sqlite3
3. Merging the data for all companies into a single DataFrame and store it in a
SQLite database.
dfs = []
for company, url in config['companies'].items():
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
table = soup.find('table')
df = pd.read_html(str(table))[0]
df['company'] = company
dfs.append(df)
df = pd.concat(dfs)
4. Writing a SQL function that updates data in the database if the same rows
already exist. You can use a trigger to accomplish this.
conn = sqlite3.connect('finance_data.db')
cur = conn.cursor()
cur.execute('''
CREATE TRIGGER IF NOT EXISTS update_finance_data
AFTER INSERT ON finance_data
BEGIN
UPDATE finance_data
SET open = new.open,
high = new.high,
low = new.low,
close = new.close,
adj_close = new.adj_close,
volume = new.volume
WHERE date = new.date AND company = new.company;
END;
''')
conn.close()
5. With these steps, you can automate the download of finance data for multiple
companies and store it in a SQLite database.
Data APIs-
Solution :-
app = Flask(__name__)
@app.route('/stocks/<date>')
def get_all_stocks_for_date(date):
conn = sqlite3.connect('finance_data.db')
cur = conn.cursor()
cur.execute('SELECT * FROM finance_data WHERE date = ?', (date,))
rows = cur.fetchall()
conn.close()
return jsonify(rows)
@app.route('/stocks/<company>/<date>')
def get_stock_for_company_and_date(company, date):
conn = sqlite3.connect('finance_data.db')
cur = conn.cursor()
cur.execute('SELECT * FROM finance_data WHERE company = ? AND date = ?',
(company, date))
rows = cur.fetchall()
conn.close()
return jsonify(rows)
@app.route('/stocks/<company>')
def get_all_stocks_for_company(company):
conn = sqlite3.connect('finance_data.db')
cur = conn.cursor()
cur.execute('SELECT * FROM finance_data WHERE company = ?', (company,))
rows = cur.fetchall()
conn.close()
return jsonify(rows)
if __name__ == '__main__':
app.run()
3. With these steps, you can create APIs that allow you to retrieve and update
finance data for multiple companies using Flask.