【发布时间】:2021-07-06 01:20:48
【问题描述】:
我正在尝试从 yahoo Finance 中抓取数据,但我只能从此链接 https://finance.yahoo.com/quote/AAPL/key-statistics?p=AAPL 的统计页面上的某些表格中获取数据。我能够从顶部表格和左侧表格中获取数据,但我无法弄清楚为什么以下程序不会从右侧表格中刮取 Beta(每月 5 年)、52 周变化、上次拆分因子等值和最后拆分日期
stockStatDict = {}
stockSymbol = 'AAPL'
URL = 'https://finance.yahoo.com/quote/'+ stockSymbol + '/key-statistics?p=' + stockSymbol
page = requests.get(URL, headers=headers, timeout=5)
soup = BeautifulSoup(page.content, 'html.parser')
# Find all tables on the page
stock_data = soup.find_all('table')
# stock_data will contain multiple tables, next we examine each table one by one
for table in stock_data:
# Scrape all table rows into variable trs
trs = table.find_all('tr')
for tr in trs:
print('tr: ', tr)
print()
# Scrape all table data tags into variable tds
tds = tr.find_all('td')
print('tds: ', tds)
print()
print()
if len(tds) > 0:
# Index 0 of tds will contain the measurement
# Index 1 of tds will contain the value
# Insert measurement and value into stockDict
stockStatDict[tds[0].get_text()] = [tds[1].get_text()]
stock_stat_df = pd.DataFrame(data=stockStatDict)
print(stock_stat_df.head())
print(stock_stat_df.info())
知道为什么这段代码没有检索这些字段和值吗?
【问题讨论】:
标签: python web-scraping beautifulsoup yahoo-finance