我想使用 python 中的 requests 包从 xpath 中提取 URL。我可以获取文本,但我尝试没有给出 URL。有人可以帮忙吗?
ipdb> webpage.xpath(xpath_url + '/text()')
['Text of the URL']
ipdb> webpage.xpath(xpath_url + '/a()')
*** lxml.etree.XPathEvalError: Invalid expression
ipdb> webpage.xpath(xpath_url + '/href()')
*** lxml.etree.XPathEvalError: Invalid expression
ipdb> webpage.xpath(xpath_url + '/url()')
*** lxml.etree.XPathEvalError: Invalid expression
我使用本教程开始:http://docs.python-guide.org/en/latest/scenarios/scrape/ http://docs.python-guide.org/en/latest/scenarios/scrape/
看起来应该很容易,但在我的搜索过程中什么也没有出现。
谢谢。
你有没有尝试过webpage.xpath(xpath_url + '/@href')
?
这是完整的代码:
from lxml import html
import requests
page = requests.get('http://econpy.pythonanywhere.com/ex/001.html')
webpage = html.fromstring(page.content)
webpage.xpath('//a/@href')
结果应该是:
[
'http://econpy.pythonanywhere.com/ex/002.html',
'http://econpy.pythonanywhere.com/ex/003.html',
'http://econpy.pythonanywhere.com/ex/004.html',
'http://econpy.pythonanywhere.com/ex/005.html'
]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)