xpath - 如何使用 Scrapy XPATH 选择这个元素?
问题描述
唯一要求:它需要引用thread-navigation
该类,因为该页面还有许多其他分页元素
<section id="thread-navigation" class="group">
<div class="float-left">
<div class="pagination talign-mleft">
<span class="pages">Pages (6):</span>
<span class="pagination_current">1</span>
<a href="I want this text?page=2" class="pagination_page">2</a>
<a href=""I want this text?page=3" class="pagination_page">3</a>
<a href=""I want this text?page=4" class="pagination_page">4</a>
<a href=""I want this text?page=5" class="pagination_page">5</a>
<a href=""I want this text?page=6" class="pagination_last">6</a>
<a href=""I want this text?page=2" class="pagination_next">Next »</a> //<--- this one
</div>
</div>
</section>
我正在尝试这样的事情:
r.xpath('//*[@class="thread-navigation" and contains (., "Next")]').get()
但它总是返回None
谢谢
解决方案
您指的不是@class
属性,而是@id
具有 value的属性thread-navigation
。所以试试这个 XPath-1.0 表达式:
r.xpath('//a[ancestor::*/@id="thread-navigation" and contains (text(), "Next")]/@href').get()
它的结果是
我想要这个文本?page=2
推荐阅读
- sftp - Chilkat:对密钥交换密码的 sftp 支持
- c# - 在 HttpWebRequest 中设置“Content-Type”标头的最佳方法?
- javascript - 如何在不刷新 php 的情况下打开、读取、关闭、更新、重新打开和读取文件
- jquery - 如何检查输入文本的值并显示正确或错误
- log4j2 - 如何将换行符附加到紧凑的 JSONLayout log4j2 条目
- android - 更新后每日用户安装量突然下降
- angular - Angular 7 PrimeNg 日历日期在 Firefox 中悬停时消失
- wpf - 如何在 WPF XAML 中将 ContentPresenter 默认内容设置为未设置
- python - 向稀疏 scipy 矩阵添加一行和一列的最有效方法?
- go - 如何使用 Now (zeit.co) 将本地文件与 golang 一起使用?