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

 
主要原因是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
%d bloggers like this: