Ken Tang
Ken Tang

Reputation: 73

want to retrieve stock company name from yahoo finance by using BeautifulSoup

I am trying to use BeautifulSoup to scrap the stock company names, however the result "IndexError: list index out of range" appears.

Belows are my codes

from bs4 import BeautifulSoup
list = ['BABA', 'APPL']
stockname = []
for i in range(len(list)):
      stock_company = "https://finance.yahoo.com/quote/"+list[i]
      soup = BeautifulSoup(requests.get(stock_company).text,"html.parser").select('h1')[0].text.strip()[10:]
      stockname.append(soup)                                         
stockname

Upvotes: 0

Views: 1894

Answers (2)

Andrej Kesely
Andrej Kesely

Reputation: 195478

The company name you can scrap from the "https://finance.yahoo.com/quote/{ticker}" URL, but however all other data (like volumes and prices) is loaded through Ajax from "https://query1.finance.yahoo.com". This example will load company name and close price:

import requests
from bs4 import BeautifulSoup
import json
from pprint import pprint

tickers = ['BABA', 'AAPL']
stockname = []
for ticker in tickers:
    stock_company = f"https://finance.yahoo.com/quote/{ticker}"
    soup = BeautifulSoup(requests.get(stock_company).text, "html.parser")
    name = soup.h1.text.split('-')[1].strip()
    ticker_data_url = f"https://query1.finance.yahoo.com/v8/finance/chart/{ticker}?region=US&lang=en-US&includePrePost=false&interval=2m&range=1d&corsDomain=finance.yahoo.com&.tsrc=finance"
    ticker_data = json.loads(requests.get(ticker_data_url).text)
    price = ticker_data['chart']['result'][0]['meta']['previousClose']
    if name:
        stockname.append( [ticker, name, price] )

pprint(stockname, width=60)

Will print:

[['BABA', 'Alibaba Group Holding Limited', 187.25],
 ['AAPL', 'Apple Inc.', 191.44]]

Apple has ticker AAPL, not APPL.

Upvotes: 2

whackamadoodle3000
whackamadoodle3000

Reputation: 6748

Why reinvent the wheel? Try the yahoo_finance module:

>>> from yahoo_finance import Share
>>> yahoo = Share('YHOO')
>>> print yahoo.get_open()
'36.60'
>>> print yahoo.get_price()
'36.84'
>>> print yahoo.get_trade_datetime()
'2014-02-05 20:50:00 UTC+0000'

Upvotes: 0

Related Questions