Python Homework
After running the code I upload, write a code using numpy and panda to sort the stock data by date(month) in recent five years.
import bs4
import requests
from bs4 import BeautifulSoup
#1#
r = requests.get(‘https://finance.yahoo.com/quote/FB/history?period1=1442707200&period2=1600560000&interval=1mo&filter=history&frequency=1mo’)
FB_soup = bs4.BeautifulSoup(r.text,’html.parser’)
FB_table =FB_soup.find_all(‘table’,{‘data-test’:”historical-prices”})
FB_stock = []
for table in FB_table:
headers =[]
rows = table.find_all(‘tr’)
for header in table.find(‘tr’).find_all(‘th’):
headers.append(header.text)
for row in table.find_all(‘tr’)[1:]:
values = []
for col in row.find_all([‘th’,’td’]):
values.append(col.text)
if values:
FB_dict = {headers[i]: values[i] for i in range(len(values))}
FB_stock.append(FB_dict)
for info in FB_stock:
print(info)
#2#
r = requests.get(‘https://finance.yahoo.com/quote/AMZN/history?period1=1442707200&period2=1600560000&interval=1mo&filter=history&frequency=1mo’)
AMZN_soup = bs4.BeautifulSoup(r.text,’html.parser’)
AMZN_table = AMZN_soup.find_all(‘table’,{‘data-test’:”historical-prices”})
r = requests.get(‘https://finance.yahoo.com/quote/AMZN/history?period1=1442707200&period2=1600560000&interval=1mo&filter=history&frequency=1mo’)
AMZN_soup = bs4.BeautifulSoup(r.text,’html.parser’)
AMZN_table = AMZN_soup.find_all(‘table’,{‘data-test’:”historical-prices”})
AMZN_stock = []
for table in AMZN_table:
AMZN_headers = []
AMZN_rows = table.find_all(‘tr’)
for header in table.find(‘tr’).find_all(‘th’):
headers.append(header.text)
for row in table.find_all(‘tr’)[1:]:
values = []
for col in row.find_all([‘th’,’td’]):
values.append(col.text)
if values:
AMZN_dict = {headers[i]: values[i] for i in range(len(values))}
AMZN_stock.append(AMZN_dict)
for info in AMZN_stock:
print(info)
#3#
r = requests.get(‘https://finance.yahoo.com/quote/GOOG/history?period1=1442707200&period2=1600560000&interval=1mo&filter=history&frequency=1mo’)
GOOG_soup = bs4.BeautifulSoup(r.text,’html.parser’)
GOOG_table = GOOG_soup.find_all(‘table’,{‘data-test’:”historical-prices”})
GOOG_stock = []
for table in GOOG_table:
GOOG_headers = []
GOOG_rows = table.find_all(‘tr’)
for header in table.find(‘tr’).find_all(‘th’):
headers.append(header.text)
for row in table.find_all(‘tr’)[1:]:
values = []
for col in row.find_all([‘th’,’td’]):
values.append(col.text)
if values:
GOOG_dict = {headers[i]: values[i] for i in range(len(values))}
GOOG_stock.append(GOOG_dict)
for info in GOOG_stock:
print(info)
#4#
r = requests.get(‘https://finance.yahoo.com/quote/AAPL/history?period1=1442707200&period2=1600560000&interval=1mo&filter=history&frequency=1mo’)
AAPL_soup = bs4.BeautifulSoup(r.text,’html.parser’)
AAPL_table = AAPL_soup.find_all(‘table’,{‘data-test’:”historical-prices”})
AAPL_stock = []
for table in AAPL_table:
AAPL_headers = []
AAPL_rows = table.find_all(‘tr’)
for header in table.find(‘tr’).find_all(‘th’):
headers.append(header.text)
for row in table.find_all(‘tr’)[1:]:
values = []
for col in row.find_all([‘th’,’td’]):
values.append(col.text)
if values:
AAPL_dict = {headers[i]: values[i] for i in range(len(values))}
AAPL_stock.append(AAPL_dict)
for info in AAPL_stock: