【发布时间】:2018-07-08 11:54:32
【问题描述】:
from lxml import html
import requests
url = 'https://www.bloomberg.com/quote/SPX:IND'
page = requests.get(url)
tree = html.fromstring(page.content)
num = tree.xpath('//*[@id="root"]/div/div/section[2]/div[1]/div/section[1]/section/section[2]/section/div[1]/span[1]/text()')
print (num)
这是我写的代码。我正在尝试从this 获取字符串 2758.82。但我得到的是。
[]
我从网站上复制了该部分的 xpath。我在这里看到了类似的问题,但它们没有帮助。我的代码有问题吗?
【问题讨论】:
-
如果你仍然没有得到你想解析的数字,除了@Arount 已经建议的之外,你还需要做一件事。你需要定义一个像
requests.get(url,headers={"User-Agent":"Mozilla/5.0"})这样的标题来让你的爬虫更像一个人。 -
谢谢!!它现在正在工作。
-
还有一件事..我如何访问像
<div style="display:inline" data-dobid="dfn"><span>some text.</span></div>这样的东西,如果<span>也有一些属性呢? -
如果您想使用可见标签,请尝试使用 selenium,它可以让您解析您想要抓取的任何项目,考虑到它们的可见形式。
标签: python python-3.x xpath web-scraping