ruby - 如何检查xpath中的节点是否有某个子x但也没有子y
问题描述
我正在尝试获取给定 github 用户的非分叉存储库。目前,我设法使用此 xpath 查询获取所有存储库:
parser.xpath("//ul[@data-filterable-for='your-repos-filter']/li/div/div/h3/a/@href").map{|repository| ...}
关键是我需要过滤掉最后一个 div 的下一个“兄弟”不是a 的那些span
,例如:
parser.xpath("//ul[@data-filterable-for='your-repos-filter']/li/div/div/h3 and not span/a/@href").map{|repository| ...}
我正在寻找的 HTML如下(检查其中一个分叉的存储库):
<li class="col-12 d-flex width-full py-4 border-bottom public fork" itemprop="owns" itemscope itemtype="http://schema.org/Code">
<div class="col-10 col-lg-9 d-inline-block">
<div class="d-inline-block mb-1">
<h3 class="wb-break-all">
<a href="/DominikAngerer/rails-boilerplate" itemprop="name codeRepository" >
rails-boilerplate</a>
</h3>
<span class="f6 text-gray mb-1">
Forked from <a class="muted-link" href="/polomasta/rails-boilerplate">polomasta/rails-boilerplate</a>
</span>
</div>
<div>
<p class="col-9 d-inline-block text-gray mb-2 pr-4" itemprop="description">
Ruby on Rails Storyblok Starter Boilerplate
</p>
</div>
什么时候不是分叉存储库,我正在寻找的那些,没有这样的<span class="f6 text-gray mb-1">
是否有可能进行这样的查询,如果可以,如何查询?
解决方案
You can use the following XPath to select the links of non-forked repositories :
//div[@class="d-inline-block mb-1"][not(./span[contains(.,"Forked from")])]//@href
Output : 17 nodes for https://github.com/DominikAngerer?tab=repositories
推荐阅读
- javascript - 如果 if/else if 中的任何条件为真,则执行代码块的聪明方法
- php - Laravel Session Flash 在控制器中使用 Artisan Cache 命令后返回 Null
- reactjs - React - 内存泄漏,ComponentWillUnmount / Axios 的问题
- c++ - 无法通过指针分配内存
- flutter - Image_picker,如何在一个功能中使用 ImageSource 相机和图库
- python - 构建具有动态字段数的 Django 优雅过滤器
- mysql - 排序查询中的“前行”
- twilio - 用于传入消息的多个 webbook 端点
- c++ - 如何将类元素添加到另一个私有类中的向量
- jenkins - 如何在文件延迟签入时安排 Jenkins 作业