'Downloading multiple tickers data from Yahoo Finance
I saw this code that Andrej Kesely posted on StackOverflow. I really find it very helpful, but I am trying to download multiple tickers instead of one ticker. Cannot scrape from table in yahoo finance
Currently, I am downloading multiple tickers for prices, and I use the ['AAPL', 'MSFT', 'AMZN'], so I tried to change
url = ["https://finance.yahoo.com/quote/AAPL/key-statistics?p=AAPL","https://finance.yahoo.com/quote/AAPL/key-statistics?p=MSFT"]
It didn't work, I was wondering if anyone know how to change it for a url. Thank you so much.
import requests
from bs4 import BeautifulSoup
url = "https://finance.yahoo.com/quote/AAPL/key-statistics?p=AAPL"
headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0"
}
soup = BeautifulSoup(requests.get(url, headers=headers).content, "html.parser")
for t in soup.select("table"):
for tr in t.select("tr:has(td)"):
for sup in tr.select("sup"):
sup.extract()
tds = [td.get_text(strip=True) for td in tr.select("td")]
if len(tds) == 2:
print("{:<50} {}".format(*tds))
Solution 1:[1]
Why not use DataReader
?
from pandas_datareader.data import DataReader # conda install pandas-datareader
data = DataReader(['AAPL', 'AMZN', 'GOOG', 'MFST'], 'yahoo', start='2015-01-01', end='2021-08-30')
print(data)
Result:
Attributes Adj Close Close \
Symbols AAPL AMZN GOOG MFST AAPL
Date
2015-01-02 24.782110 308.519989 523.373108 22.000000 27.332500
2015-01-05 24.083958 302.190002 512.463013 23.700001 26.562500
2015-01-06 24.086227 295.290009 500.585632 23.700001 26.565001
2015-01-07 24.423975 298.420013 499.727997 21.100000 26.937500
2015-01-08 25.362394 300.459991 501.303680 21.100000 27.972500
... ... ... ... ... ...
2021-08-23 149.710007 3265.870117 2821.989990 0.000800 149.710007
2021-08-24 149.619995 3305.780029 2847.969971 0.000700 149.619995
2021-08-25 148.360001 3299.179932 2859.000000 0.000800 148.360001
2021-08-26 147.539993 3316.000000 2842.459961 0.000500 147.539993
2021-08-27 148.600006 3349.629883 2891.010010 0.000400 148.600006
Solution 2:[2]
you can try also yfinace:
import yfinance as yf
etf = ['AXP','AAPL','BA','CAT','CSCO','CVX','XOM','GS','HD','IBM','INTC','JNJ','KO'] #and any tickers you'd add to be retrived
tit = yf.download(tickers=etf, period='max')
Solution 3:[3]
You should be looking for:
- https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT
- https://finance.yahoo.com/quote/AMZN/key-statistics?p=AMZN
- https://finance.yahoo.com/quote/APPL/key-statistics?p=APPL
In that case, you can use the f-string:
tickers = ['AAPL', 'MSFT', 'AMZN']
for tick in tickers:
print(f"https://finance.yahoo.com/quote/{tick}/key-statistics?p={tick}")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rene |
Solution 2 | Simon |
Solution 3 |