xquery - 使用 Xidel 提取同一行的 href 和 text,仅限特定链接
问题描述
我正在尝试为<a>
html 页面中的多个链接提取标签内的链接(href)和文本。
我只想要特定的链接,我通过子字符串匹配。
我的html示例:
<a href="/this/dir/1234/">This should be 1234</a> some other html
<a href="/this/dir/1236/">This should be 1236</a> some other html
<a href="/about_us/">Not important link</a> some other html
我正在使用 Xidel,它可以让我避免使用正则表达式。这似乎是最简单的工作。
到目前为止我所拥有的:
xidel -e "//a/(@href[contains(.,'/this/dir')],text())"
它基本上可以工作,但仍然存在两个问题:
- 我得到由换行符分隔的数据。我想把它放在同一条线上。
- 每个链接文本都被返回,所以我也得到文本“不重要的链接”。
获得输出的推荐方法是什么
/this/dir/1234 ; This should be 1234
/this/dir/1236 ; This should be 1236
感谢任何反馈/提示。
编辑:
Martin 提供的解决方案是 99%。没有输出换行符,所以我使用 awk 用换行符替换虚拟文本。
注意:我在窗户上。
xidel myhtml.htm -e "string-join(//a[contains(@href, '/this/dir')]!(@href || ' ; ' || .), 'XXX')" | awk -F "XXX" "{$1=$1}1" "OFS=\n"
解决方案
您可以将条件移动到谓词中,例如//a[contains(@href, '/this/dir')]!(@href, string())
。至于结果格式,如果将所有内容委托给 XQuery 会发生什么
string-join(//a[contains(@href, '/this/dir')]!(@href || ' ; ' || .), ' ')
推荐阅读
- angular - Angular 6 - Sonarqube 覆盖率报告始终为 0,但业力显示覆盖率
- google-cloud-platform - 如何知道我在 GCP 中拥有的所有权限?
- node.js - 如何更改 api 响应
- cors - Google Cloud IAP CORS 政策阻止我的网络应用调用受 IAP 保护的 API
- javascript - Bitrix24 BX24.callBatch js 语法
- c# - Context.update() 无法跟踪实体类型“模型”的实例,因为已在跟踪另一个具有键值“{Id: 12}”的实例
- regex - 从文件中搜索和替换多行
- java - Flutter中移动应用主屏幕的小部件
- django - 如何在 django 上检索使用 google 登录的用户的照片个人资料 url
- flutter - 如何在 dart 的 GET-request 中使用 queryParamets 将参数放入 url?