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))