使用 Nokogiri 的一些例子有哪些?

发布于 2024-12-21 03:16:22 字数 69 浏览 3 评论 0原文

我试图理解Nokogiri。有谁有 Nokogiri 解析/抓取的基本示例的链接,显示生成的树。我认为这确实有助于我的理解。

I am trying to understand Nokogiri. Does anyone have a link to a basic example of Nokogiri parse/scrape showing the resultant tree. Think it would really help my understanding.

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

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

发布评论

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

评论(1

赠佳期 2024-12-28 03:16:22

使用 IRB 和 Ruby 1.9.2:

加载 Nokogiri:

> require 'nokogiri'
#=> true

解析文档:

> doc = Nokogiri::HTML('<html><body><p>foobar</p></body></html>')
#=> #<Nokogiri::HTML::Document:0x1012821a0
      @node_cache = [],
      attr_accessor :errors = [],
      attr_reader :decorators = nil

Nokogiri 喜欢格式良好的文档。请注意,它添加了 DOCTYPE 因为我解析为文档。也可以解析为文档片段,但这非常专业。

> doc.to_html
#=> "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body><p>foobar</p></body></html>\n"

使用 CSS 搜索文档以查找第一个

节点并获取其内容:

> doc.at('p').text
#=> "foobar"

使用不同的方法名称执行相同的操作:

> doc.at('p').content
#=> "foobar"

在文档中搜索所有

< /code> 节点位于 标签内,并获取第一个节点的内容。 search 返回一个节点集,它就像一个节点数组。

> doc.search('body p').first.text
#=> "foobar"

这是很重要的一点,也是几乎每个人第一次使用 Nokogiri 时都会遇到的问题。 search 及其 cssxpath 变体返回一个 NodeSet。 NodeSet.textcontent 将所有返回节点的文本连接到一个字符串中,这使得再次拆分变得非常困难。

使用稍微不同的 HTML 有助于说明这一点:

> doc = Nokogiri::HTML('<html><body><p>foo</p><p>bar</p></body></html>')
> puts doc.to_html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<p>foo</p>
<p>bar</p>
</body></html>

> doc.search('p').text
#=> "foobar"

> doc.search('p').map(&:text)
#=> ["foo", "bar"]

返回到原始 HTML...

更改节点的内容:

> doc.at('p').content = 'bar'
#=> "bar"

将解析后的文档作为 HTML 发出:

> doc.to_html
#=> "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body><p>bar</p></body></html>\n"

删除节点:

> doc.at('p').remove
#=> #<Nokogiri::XML::Element:0x80939178 name="p" children=[#<Nokogiri::XML::Text:0x8091a624 "bar">]>
> doc.to_html
#=> "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body></body></html>\n"

至于抓取,有很多关于使用的问题Nokogiri 用于从网站中分离 HTML。在 StackOverflow 中搜索“nokogiri 和 open-uri”应该会有所帮助。

Using IRB and Ruby 1.9.2:

Load Nokogiri:

> require 'nokogiri'
#=> true

Parse a document:

> doc = Nokogiri::HTML('<html><body><p>foobar</p></body></html>')
#=> #<Nokogiri::HTML::Document:0x1012821a0
      @node_cache = [],
      attr_accessor :errors = [],
      attr_reader :decorators = nil

Nokogiri likes well formed docs. Note that it added the DOCTYPE because I parsed as a document. It's possible to parse as a document fragment too, but that is pretty specialized.

> doc.to_html
#=> "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body><p>foobar</p></body></html>\n"

Search the document to find the first <p> node using CSS and grab its content:

> doc.at('p').text
#=> "foobar"

Use a different method name to do the same thing:

> doc.at('p').content
#=> "foobar"

Search the document for all <p> nodes inside the <body> tag, and grab the content of the first one. search returns a nodeset, which is like an array of nodes.

> doc.search('body p').first.text
#=> "foobar"

This is an important point, and one that trips up almost everyone when first using Nokogiri. search and its css and xpath variants return a NodeSet. NodeSet.text or content concatenates the text of all the returned nodes into a single String which can make it very difficult to take apart again.

Using a little different HTML helps illustrate this:

> doc = Nokogiri::HTML('<html><body><p>foo</p><p>bar</p></body></html>')
> puts doc.to_html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<p>foo</p>
<p>bar</p>
</body></html>

> doc.search('p').text
#=> "foobar"

> doc.search('p').map(&:text)
#=> ["foo", "bar"]

Returning back to the original HTML...

Change the content of the node:

> doc.at('p').content = 'bar'
#=> "bar"

Emit a parsed document as HTML:

> doc.to_html
#=> "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body><p>bar</p></body></html>\n"

Remove a node:

> doc.at('p').remove
#=> #<Nokogiri::XML::Element:0x80939178 name="p" children=[#<Nokogiri::XML::Text:0x8091a624 "bar">]>
> doc.to_html
#=> "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body></body></html>\n"

As for scraping, there are a lot of questions on SO about using Nokogiri for tearing apart HTML from sites. Searching StackOverflow for "nokogiri and open-uri" should help.

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