【问题标题】:Loading an hpricot element with a chunk of html用一段 html 加载 hpricot 元素
【发布时间】:2010-12-26 16:20:43
【问题描述】:

有没有办法将一段 html 加载到 Hpricot::Doc 对象中?

我正在尝试从页面中解析自定义标签中的各种 html 块。

如果我有:

<foo>
  <b>here is some stuff</b>
  <table>
    <tr>
      <td>one</td>
      <td>two</td>
    </tr>
    <tr>
      <td>three</td>
      <td><four</td>
    </tr>
  </table>
</foo>

我希望能够在 Hpricot::Doc 对象中获取 foo 及其内容,因为我需要进行一些额外的处理并最终交换()它,以便 foo 及其所有子对象在文件。

我知道我可以通过 foo 的子代进行迭代,但我希望有一种方法可以将它们全部抓取到一个块中以保持干净。此外,可能有也可能没有属性。会有很多项目,每个项目都有一段 HTML,但没有 foo 项目会包含另一个 foo 项目。

这有可能吗? 最后,我从 Hpricot 开始,但如果能有所作为,我愿意接受 Nokogiri。

【问题讨论】:

    标签: html ruby parsing hpricot custom-tags


    【解决方案1】:

    我不清楚你遇到了什么问题。

    您可以以任何您喜欢的方式传递 hpricot html。

    来自Readme

    doc = Hpricot("<p>A simple <b>test</b> string.</p><foo>foo content</foo>")
    

    你可以搜索 foo 并交换它

    doc.search("//foo").first.swap "<blink>not foo</blink>"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 2022-10-15
      相关资源
      最近更新 更多