这是URL我正想试一下呢我试图获取文章的正文内容; “一起看电视……”。我尝试过以下表达式:
[In 1]:response.xpath("//*[contains(@class, 'text parbase')]//text()").extract()
[Out 1]:[]
[In 2]:response.xpath("//*[contains(@class, 'text')]//text()").extract()
[Out 2]: [u'\n',
u'\n',
u'\n\n',
u'\n $CQ(function() {\n CQ_Analytics.SegmentMgr.loadSegments("/etc/segmentation");\n CQ_Analytics.ClientContextUtils.init("","/content/corporate/us/en/insights/journal-of-measurement/volume-1-issue-2/nott-alone-is-ott-making-it-cool-again-to-watch-tv-together");\n\n \n });\n',
u'\n']
[In 3]:response.xpath("//p//text()").extract()
[Out 3]:[u'X']
它们似乎都不包含我想要的东西。我在这里做错了什么吗?如果这个问题已经得到解答,我很抱歉,我已经尽力寻找答案,但还没有找到任何东西。任何帮助将不胜感激。 谢谢!
最佳答案
网站的 HTML 输出似乎存在某种问题,Scrapy 解析器无法渲染该部分。您可以使用正则表达式提取内容来修复此问题:
import re
from scrapy import Selector
section = re.match(r'.*(<div.*?parbase toptext.*?)</div>', response.body, re.DOTALL).group(1)
Selector(text=section).xpath('//text()').extract()
关于python - Scrapy xpath 不起作用(也许是 parbase 的东西?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46316456/