首页 > 解决方案 > xpath 无法识别标签

问题描述

我正在尝试使用 xpath 从论坛中抓取 reddit 帖子。我希望蜘蛛实现的功能之一是从当前页面完成抓取后立即自动转到下一页。页面 html 代码如下所示:

<span class="next-button"><a href="https://www.reddit.com/r/InteriorDesign/?count=975&amp;after=t3_8ol7yp" rel="nofollow next" >next &rsaquo;</a></span>

我将 xpath 选择器用作: response.xpath("//a[@class = 'next-button']") 但它没有给我任何回报。有人可以帮我弄清楚为什么吗?

谢谢!豪

标签: javascripthtmlxpathweb-scrapingscrapy

解决方案


@class属性在span元素上,而不是在a链接元素上。因此,将您的 XPath 更改为

response.xpath("//span[@class = 'next-button']/a")

选择a

response.xpath("//span[@class = 'next-button']/a/@href")

获取链接地址。


推荐阅读