Scrapy XPath页面中有Unicode时不正确

时间:2016-10-15 18:42:12

标签: python xpath scrapy

我想获得所有具有类别类别的div。

请看这个页面:www.postkhmer.com/ព័ត៌មានជាតិ

enter image description here

scrapy shell中的

scrapy shell 'www.postkhmer.com/ព័ត៌មានជាតិ'

enter image description here

如你所见,我只有2个元素。

scrapy fetch --nolog http://www.postkhmer.com/ព័ត៌មានជាតិ > page.html scrapy shell ./page.html response.xpath('//div[@class="category"]') 仍然只有2个元素。但是当我在Sublime中打开page.html时。

我有15场比赛: enter image description here

最有趣的部分是:当我从第二类删除锚链接时:

enter image description here

我再次在scrapy shell中运行response.xpath('//div[@class="category"]'),我有3个元素:

enter image description here

我喜欢到底是什么地方!?有人可以帮我解决这个问题吗?

我已在here上传了您要在本地测试的文件。

2 个答案:

答案 0 :(得分:0)

将页面保存到本地文件page.html时,跳过包含编码信息的http标头。 稍后,当您使用scrapy或sublime打开此文件时,他们不知道文档的原始编码是什么。

推荐:从未使用保存到文件中的文档进行解析。

答案 1 :(得分:0)

这里只能发生两件事。要么html格式不正确,scrapy无法解析它,或者scrapy和编码有问题。我认为第一个更有可能。 http://www.freeformatter.com/html-validator.html有点放弃它。

由于它适用于Chrome,我建议使用selenium使浏览器修复代码并从中删除元素。我没有测试,但也许scrapy-splash可以产生同样的效果。