javascript - xpath 无法识别标签
问题描述
我正在尝试使用 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")
获取链接地址。
推荐阅读
- java - 如何使用 JSP Servlet Login 设置 Tomcat 服务器
- flutter-web - 为什么我的 Flutter Web 应用程序在运行 webdev serve 时无法运行
- python - 是否可以为 OneHotEncoder 中的某些列指定 handle_unknown = 'ignore' 并为其他列指定 'error'?
- google-cloud-run - 如何在 GKE 上使用 Cloud Run 与 Pub/Sub 集成
- react-native - 如何避免不变的违反:文本字符串必须在一个
零件 ?我对 react-native 比较陌生 - python - 使用正斜杠定义仅位置参数时出现语法错误
- python - 带有 Model.Fit() 的 Keras InvalidArgumentError
- ruby - 以下两个代码片段之间有什么区别吗?
- c# - 使用 GraphQ 避免 HTTP 开销
- swift - 在另一个不在标签栏中的视图控制器中显示标签栏项目