我试图从yahoo finance中获取数据,但我只能从统计页面上的某些表中获取数据。我可以从上表和左表中获取数据,但我不明白为什么下面的程序不能从右表中获取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())
知道这段代码为什么没有检索这些字段和值吗
要从Yahoo服务器获得正确的响应,请设置
User-Agent
HTTP头:印刷品:
相关问题 更多 >
编程相关推荐