我正在尝试使用 xpath 从论坛中抓取 reddit 帖子。 我希望蜘蛛实现的功能之一是,一旦完成从当前页面的抓取,就自动转到下一页。 页面 html 代码如下所示:
<span class="next-button"><a href="https://www.reddit.com/r/InteriorDesign/?count=975&after=t3_8ol7yp" rel="nofollow next" >next ›</a></span>
我使用 xpath 选择器作为: response.xpath("//a[@class = 'next-button']") 但它没有给我任何返回。有人可以帮我找出原因吗?
谢谢! 郝
最佳答案
@class
属性位于 span
元素上,而不是 a
链接元素上。因此,将您的 XPath 更改为
response.xpath("//span[@class = 'next-button']/a")
选择a
或
response.xpath("//span[@class = 'next-button']/a/@href")
获取链接地址。
关于javascript - xpath 无法识别标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50917628/