I'm kinda new to python and having some problem with my code. Would appreciate any suggestions on what I try to do.
import pandas as pd
from bs4 import BeautifulSoup
import requests
def trade():
tickers = ["AAPL","AMZN", "INTC", "MSFT", "SNAP"]
for ticker in tickers:
url = "http://finance.yahoo.com/quote/%s?p=%s"%(ticker,ticker)
res = requests.get(url)
soup = (BeautifulSoup(res.content, 'lxml'))
table = soup.find_all('table')[0]
df = pd.read_html(str(table))
print("DF")
print(df)
df_string = str(df)
print(df_string_parse)
print(type(df_string_parse))
When I look at df, it displays string like this
[ 0 1
0 Previous Close 167.37
1 Open 169.79
2 Bid 157.23 x 300
3 Ask 157.29 x 500
4 Day's Range 169.00 - 173.09
5 52 Week Range 134.84 - 180.10
6 Volume 51124085
7 Avg. Volume 33251246]
What I want to do, is to store each ticker's table into dataframe, and merge them together like below. Or I guess if there is a way, I Could turn that into dictionary so that I could use its variable more easily.
APPL AMAZN INTC MSFT SNAP
Previous Close
Open
Bid
Ask
Day's Range
Volume
Avg. Volume
For now, there are 2 problems that I face:
- how to turn the table into data frame and/or dictionary after using pd.read_html(str(table))
- how to store each ticker's results separately eventually to merge them together? I know how to use for loop to read them one by one, but I don't seem to know how to store them in such way.