【问题标题】:Get attribute value in html using xpath with xmllint使用带有 xmllint 的 xpath 在 html 中获取属性值
【发布时间】:2023-02-20 23:25:13
【问题描述】:

给定一个 html 文档,我想使用 xmllintxpath 表达式来获取一组属性(已经匹配)它们的对应值。例如,检索 a 元素中的 href 属性值(不是包括其名称的整个属性)的值,并获取链接的 url。

我尝试使用 '//a/@href' 来获取属性,但这不仅给了我值,还给了我属性名称。在其他一些帖子中,据说您必须以​​这种方式使用 string 函数 'string(//a/@href)',但是当比较两个输出时,我得到不同数量的匹配项。例如:

xmllint --html --xpath '//a/@href' <(curl -L www.html-tidy.org) | wc
 

给出:

HTML parser error : Tag nav invalid
    <nav><ul>
        ^
     50      50    2161

所以有 50 个(错误来自xmllint 解析 html-tidy 的网络索引)。

当我使用 string 函数时:

xmllint --html --xpath 'string(//a/@href)' <(curl -L www.html-tidy.org) | wc
 

它只给出了 50 场比赛中的一场。

    <nav><ul>
        ^
      1       1      26

那么我怎样才能正确使用它(或者这是正确的使用方法)? 谢谢

【问题讨论】:

    标签: xpath xmllint


    【解决方案1】:

    对于 realLife©®™HTML,最好使用

    xidel -e '//a/@href' https://www.html-tidy.org
    

    输出

    https://github.com/htacg/html-tidy.org/tree/gh-pages/plus/_posts
    https://github.com/htacg/tidy-html5/issues
    https://github.com/htacg/tidy-html5/pulls
    [...]
    

    【讨论】:

      猜你喜欢
      • 2018-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      • 2015-11-02
      • 2016-04-22
      • 1970-01-01
      • 2021-02-01
      相关资源
      最近更新 更多