【发布时间】:2017-09-16 19:00:31
【问题描述】:
我正在尝试使用 XPath 从晨星关键比率页面中提取任何给定股票的数据。我有在谷歌浏览器的 XPath Helper 工具栏插件中返回结果的完整路径,但是当我将它插入我的代码时,我得到一个空白列表返回。
如何获得我想要返回的结果?这甚至可能吗?我是否使用了错误的方法?
非常感谢任何帮助!
我想要返回的数据片段:
AMD 关键比率示例:
我的代码:
from urllib.request import urlopen
import os.path
import sys
from lxml import html
import requests
page = requests.get('http://financials.morningstar.com/ratios/r.html?t=AMD®ion=USA&culture=en_US')
tree = html.fromstring(page.content)
rev = tree.xpath('/html/body/div[1]/div[3]/div[2]/div[1]/div[1]/div[1]/table/tbody/tr[2]/td[1]')
print(rev)
代码结果:
[]
XPath Helper 的期望结果:
谢谢, 不是欧拉
【问题讨论】:
标签: python-3.x xpath web-scraping request