使用 html 块加载 hpricot 元素

发布于 2024-08-15 01:19:48 字数 692 浏览 3 评论 0原文

有没有办法将一段 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>

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

我知道我可以迭代 foo 的子级,但我希望有一种方法可以将所有内容集中到一个块中以保持干净。此外,可能有也可能没有属性。将会有许多项目,每个项目都有一块 HTML,但没有 foo 项目会包含另一个 foo 项目。

这有可能吗? 最后,我从 Hpricot 开始,但如果 Nokogiri 能有所作为的话,我对 Nokogiri 持开放态度。

is there a way to load a chunk of html into an Hpricot::Doc object?

I am trying to parse various chunks of html within custom tags from a page.

so if I have:

<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>

I would love to be able to get foo and it's contents within an Hpricot::Doc object because I am going to need to do some additional processing and eventually swap() it so that foo and all its children are replaced in the document.

I know I can iterate by the children of foo, but I was hoping there was a way to grab it all in one chunk to keep things clean. Also, may or may not have attributes. There will be many items, each with a chunk of HTML, but no foo item will contain another foo item.

Is this at all possible?
Lastly, I started with Hpricot, but I am open to Nokogiri if it would make a difference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小苏打饼 2024-08-22 01:19:48

我不清楚你遇到了什么麻烦。

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

自述文件

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

您可以搜索 foo 并交换它

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

I'm not clear on what you are having trouble with.

You can pass hpricot your html any way you like.

From the Readme

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

You can search for foo and swap it

doc.search("//foo").first.swap "<blink>not foo</blink>"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文