伙计们,我想从雅虎财经的公司所在国家/地区抓取 - 美国,该公司位于雅虎财经的个人资料页面中。链接是:
https://finance.yahoo.com/quote/AAPL/profile?p=AAPL
我尝试了这段代码,但无法提取它。我是抓取数据的新手,如果您能帮助我,我将不胜感激。
我的代码:
import requests
from lxml import html
xp = "//span[text()='Sector']/following-sibling::span[1]"
symbol = 'AAPL'
url = 'https://finance.yahoo.com/quote/' + symbol + '/profile?p=' + symbol
page = requests.get(url)
tree = html.fromstring(page.content)
d = {}
我更喜欢 lxm 和 requests,并且没有使用过 beautifulsoup,所以更喜欢在代码库中指出。
非常感谢任何帮助。
最佳答案
看看这是否适合您:
xpp = tree.xpath('//div[@data-reactid=7]/p/text()[3]')[0].strip()
xpp
输出:
'United States'
关于python - 从雅虎财经抓取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59235898/