0

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.

  • if it was some syntax error, you would get an error, do you get any errors when running the code? if so post the full error traceback in the question (use [edit]). otherwise what do you expect the code to do? what do you mean by real time? could it be that yahoo finance haven't updated their data yet? – Matiiss Aug 02 '21 at 14:14
  • 1
    when accesing a webpage multiple times, you should always check if the site has an API to use instead. They normally want you to use apis over webscraping. – ph140 Aug 02 '21 at 14:16
  • No, I meant syntactic error. This does not do what it supposed to do. There are no error messages when I run the code. The output is that it collects prices but does not update when the data on the page update. That's my problem – Dominik Pucuła Aug 02 '21 at 14:18
  • 1
    There are API & also library (like yfinance) for using Yahoo Finance Data with Python. You don't have to go through a "wild scrapping" – Benoit Drogou Aug 02 '21 at 14:20
  • Can you get live data on stock prices with yfinance library? I cannot find it anywhere – Dominik Pucuła Aug 02 '21 at 14:56

0 Answers0