甬Python3爬取网页遇到AttributeError: 'HTMLResponse' object has no attribute 'xpath'

遇到的问题如下:

img


且已经帮我指出了是这段代码错误

img

这个是为什么啊?请各位指点(第一次发帖请教,如有问题还请见谅)

你需要将response.text用lxml中etree的HTML方法先转换一下,再用xpath解析.类似这样:

respose = requests.get(url, headers=headers)
respose.encoding = 'utf-8'
html = etree.HTML(respose.text)
print(html)

如有帮助,请点击我回答右上方采纳按钮,给予采纳支持~~

你的HTMLResponse是网页内容吧,它只是HTML代码,你要用bs4这类解析HTML