gpt4 book ai didi

python - 当页面中有 Unicode 时,Scrapy XPath 不正确

转载 作者:行者123 更新时间:2023-12-03 16:52:26 24 4
gpt4 key购买 nike

我想获取所有具有类别类的 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

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

enter image description here

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

enter image description here

我就像什么鬼!?有人可以帮我解决这个问题吗?

我已将文件上传到 here如果您想在本地进行测试。

最佳答案

这里只能发生两件事。要么 html 格式错误,scrapy 无法解析它,要么scrapy 和编码存在一些问题。我觉得第一种可能性更大。 http://www.freeformatter.com/html-validator.html有点放弃它。

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

关于python - 当页面中有 Unicode 时,Scrapy XPath 不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40062836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com