【发布时间】:2020-02-25 03:11:40
【问题描述】:
我是网络抓取的新手,我正在尝试为 AAPL 抓取 yahoo Finance 的“统计”页面。这是链接:https://finance.yahoo.com/quote/AAPL/key-statistics?p=AAPL
这是我到目前为止的代码......
from bs4 import BeautifulSoup
from requests import get
url = 'https://finance.yahoo.com/quote/AAPL/key-statistics?p=AAPL'
response = get(url)
soup = BeautifulSoup(response.text, 'html.parser')
stock_data = soup.find_all("table")
for stock in stock_data:
print(stock.text)
当我运行它时,我会返回页面上的所有表格数据。但是,我只需要每个表中的特定数据(例如“市值”、“收入”、“测试版”)。
我尝试通过执行print(stock[1].text) 来弄乱代码,以查看是否可以将返回的数据量限制为每个表中的第二个值,但这会返回错误消息。使用 BeautifulSoup 是否走在正确的轨道上,还是需要使用完全不同的库?为了只返回特定数据而不是页面上的所有表格数据,我需要做什么?
【问题讨论】:
标签: python web-scraping beautifulsoup data-science