我正在尝试从 http://www.cellartracker.com/wine.asp?iWine=91411 的“drink between: 2005 - 2013”文本中抓取文本“2005-2013”使用 xpath/lxml,我只能为这个网站上的其他一些页面做这件事,而不是这个。不确定我做错了什么/如果我从元素复制的 xpath 不正确
它告诉我:
print(content_divs[0].text_content().strip())
IndexError: list index out of range
这是我的代码:
import requests, lxml.html
page = requests.get('http://www.cellartracker.com/wine.asp?iWine=91411')
html = lxml.html.fromstring(page.content)
content_divs = html.xpath('//*[@id="wine_copy_inner"]/p/a[4]')
print(content_divs[0].text_content().strip())
感谢您的帮助!!!
最佳答案
如果你想得到 "2005 - 2013"
你可以使用下面的代码
content = html.xpath('//a[@title="Source: Community"]/text()')
关于python - 使用 xpath/lxml 抓取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45336668/