【问题标题】:Search by ID label in an HTML with hxselect使用 hxselect 在 HTML 中按 ID 标签搜索
【发布时间】:2018-02-12 04:27:13
【问题描述】:

我正在使用 hxselect 在 bash 脚本中解析 HTML 文件“index.html”。标签上可能有也可能没有以下ID,但如果有,我需要获取内容。

例如。

我将“someID”作为唯一要搜索的信息和以下 HTML

<html>
  <body>
    <div id="someID" class="c">some other html stuff</div>
  </body>
</html>

我只需要得到“一些其他 html 的东西”作为结果

我做不到 cat index.html | hxselect -c 'someID'

【问题讨论】:

    标签: html bash parsing


    【解决方案1】:

    匹配id属性:

    hxselect -c '#someID' < file.html
    

    匹配class属性:

    hxselect -c '.c' < file.html
    

    div标签与路径匹配:

    hxselect -c html body div < file.html
    

    只匹配div:

    hxselect -c div < file.html
    

    所有情况下的输出:

    其他一些 html 的东西

    见:man hxselect

    【讨论】:

    • 如果你得到“结束标签 tag1 与开始标签 tag2 不匹配”try pipinghxclean first
    【解决方案2】:

    对于这种情况,我会推荐 xmlstarlet 工具:

    xmlstarlet sel -t -v "//div[@id='someID']" -n index.html
    

    输出:

    some other html stuff
    

    http://xmlstar.sourceforge.net/doc/UG/xmlstarlet-ug.html#idm47077139652416


    xmllint 工具也可以做到这一点:

    xmllint --html --xpath "//div[@id='someID']/text()" index.html
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多