嗨,我是scrapy的菜鸟。我试图从以下页面抓取文章(内容、机构名称、通讯员等):http : //timesofindia.indiatimes.com/topic/Startup
问题是我的蜘蛛返回了大多数文章的正确结果,但对于机构名称为“路透社”的文章(例如 - http://timesofindia.indiatimes.com/business/international-business/novartis-roche-back -french-gene-therapy-start-up-vivet/articleshow/58511702.cms),它只返回一堆转义字符而不是内容(虽然它确实返回标题和机构名称)。这是我的 xpath 变量:
main_path=response.xpath('//div[@class="main-content"]')
yield {
'Headline':"".join(main_path.xpath('.//h1[@class="heading1"]/text()').extract(),
'Correspondent':"".join(main_path.xpath('.//span[@class="auth_detail"]/text()').extract()),
'Agency':"".join(main_path.xpath('.//span[@itemprop="name"]/text()').extract()),
'ArticleContent':(main_path.xpath('.//div[@class="Normal"]/text()').extract()),
}
你们能帮我弄清楚为什么我会面临这个问题吗?谢谢
解决方案:/
在text()
xpath 中插入一秒之前
'ArticleContent':(main_path.xpath('.//div[@class="Normal"]//text()').extract()),
解释
路透社<p>
在他们的文章内容中有额外的标签。虽然../text()
只捕获同一节点/标签内的文本,但..//text()
对子标签/子节点也是如此。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句