深度阅读

解决 XPATH在 Chrome上好用,但不适用于 Scrapy

作者
作者
2023年08月22日
更新时间
4.3 分钟
阅读时间
0
阅读量

主要原因是html存在错误,scrapy不能很好的解析,所以这时候就是bs4上来就行了。 找到了答案。事实证明 HTML 已损坏,Scrapy 无法自行修复,因此需要 Beautiful Soup 的帮助。我这样做: 先安装beautifulsoup4 pip install beautifulsoup4 from scrapy.selector import Selector from bs4 import BeautifulSoup fixed\_html = str(BeautifulSoup(response.body, "lxml")) print (Selector(text=fixed\_html).xpath('//\*'))  参考链接   https://stackoverflow.com/questions/42807538/xpath-works-in-chrome-but-not-in-scrapy

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。