【问题标题】:Xidel extract data inside the tag -- raw outputXidel 提取标签内的数据——原始输出
【发布时间】:2018-04-18 18:27:21
【问题描述】:

很高兴成为 StackOverflow 的一员,在这里潜伏了很长时间。

我需要解析两个标签之间的文本,目前我发现了一个很棒的工具,叫做Xidel

我需要在两者之间解析文本

<div class="description">
Text. <tag>Also tags.</tag> More text.
</div>

但是,所述文本中可以包含 HTML 标记,我希望它们以原始格式打印出来。所以使用如下命令:

xidel --xquery '//div[@class="description"]' file.html

得到我:

Text. Also tags. More text.

而且我需要它保持原样,所以:

Text. <tag>Also tags.</tag> More text.

我怎样才能做到这一点?

问候,R

【问题讨论】:

    标签: html linux bash parsing xidel


    【解决方案1】:

    可以通过 Xidel 以多种方式完成,这就是我如此喜欢它的原因。

    HTML 模板:

    xidel -s file.html -e "<div class='description'>{inner-html()}</div>"
    

    XPath:

    xidel -s file.html -e "//div[@class='description']/inner-html()"
    

    CSS:

    xidel -s file.html -e "inner-html(css('div.description'))"
    

    顺便说一句,在 Linux 上:将双引号换成单引号,反之亦然。

    【讨论】:

      【解决方案2】:

      您可以通过添加--output-format=xml 选项来显示标签。

      xidel --xquery '//div[@class="description"]' --output-format=xml file.html 
      

      【讨论】:

      • 这将为文件创建一个新的 xml 模板,包括 &lt;div class="description"&gt; 标记本身。我非常怀疑这就是 RomanM 想要的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-02
      • 1970-01-01
      • 2022-01-16
      • 2016-11-28
      • 2018-11-10
      • 1970-01-01
      相关资源
      最近更新 更多