I have a problem which I don't know how to solve (I'm a beginner in coding). This program is supposed to scrape stock price data from yahoo finance:
import bs4
from bs4 import BeautifulSoup
import requests
import pandas as pd
import datetime as dt
def real_time_price(stock_code):
url = 'https://finance.yahoo.com/quote/' + stock_code + '/'
r = requests.get(url)
web_content = BeautifulSoup(r.text, 'lxml')
web_content = web_content.find('div', {'class':'My(6px) Pos(r) smartphone_Mt(6px)'})
web_content = web_content.find('span').text
if web_content==[]:
web_content = '999999'
return web_content
LA = ['AAPL', 'FB', 'F', 'AMZN', 'GOOG']
for step in range(1,101):
price = []
col = []#Lista, która dodaje dane do df
time_stamp = dt.datetime.now()
time_stamp = time_stamp.strftime('%Y-%m-%d %H:%M:%S')
for stock_code in LA:
price.append(real_time_price(stock_code))
col = [time_stamp]
col.extend(price)
df = pd.DataFrame(col)
df = df.T
df.to_csv('realtimestockdata.csv', mode = 'a', header = False)
print(col)
But it seems that it does not update when it's running is there some syntactic error in that that I missed?
All responses are really appriciated, thank you.