【问题标题】:Is there a way to pretty print the Nokogiri::HTML::Document object? [duplicate]有没有办法漂亮地打印 Nokogiri::HTML::Document 对象? [复制]
【发布时间】:2013-12-26 07:48:32
【问题描述】:

有没有办法以漂亮的格式(而不是 HTML)输出 Nokogiri::HTML::Document 对象?我希望能够看到随着级别变深而缩进的对象。就像使用 awesome_print (试过了 - 不起作用)。谢谢!

当我运行以下命令以通过以下方式实例化 Nokogiri 对象时,当前在控制台中:

irb(main):105:0> html = open("http://www.google.com")
=> #<Tempfile:/var/folders/kx/nwfjzgd153g071ykz0mtgd0r0000gp/T/open-uri20131225-35224-y57yx3>
irb(main):106:0> document = Nokogiri::HTML(html.read)

它会产生以下难以阅读的 blob:

=> #<Nokogiri::HTML::Document:0x3ff87d83d7d0 name="document" children=[#<Nokogiri::XML::DTD:0x3ff87d83d2f8 name="html">, #<Nokogiri::XML::Element:0x3ff87d83cf10 name="html" attributes=[#<Nokogiri::XML::Attr:0x3ff87d83ce98 name="itemscope">, #<Nokogiri::XML::Attr:0x3ff87d83ce84 name="itemtype" value="http://schema.org/WebPage">] children=[#<Nokogiri::XML::Element:0x3ff87d83c77c name="head" children=[#<Nokogiri::XML::Element:0x3ff87d83c4c0 name="meta" attributes=[#<Nokogiri::XML::Attr:0x3ff87d83c434 name="content" value="Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for.">, #<Nokogiri::XML::Attr:0x3ff87d83c420 name="name" value="description">]>, #<Nokogiri::XML::Element:0x3ff87d83371c name="meta" attributes=[#<Nokogiri::XML::Attr:0x3ff87d8335b4 name="content" value="noodp">, #<Nokogiri::XML::Attr:0x3ff87d8335a0 name="name" value="robots">]>, #<Nokogiri::XML::Element:0x3ff87d8325c4 name="meta" attributes=[#<Nokogiri::XML::Attr:0x3ff87d832510 name="itemprop" value="image">, #<Nokogiri::XML::Attr:0x3ff87d8324e8 name="content" value="/images/google_favicon_128.png">]>, #<Nokogiri::XML::Element:0x3ff87d82f964 name="title" children=[#<Nokogiri::XML::Text:0x3ff87d82f6d0 "Google">]>, #<Nokogiri::XML::Element:0x3ff87d82f478 name="script" children=[#<Nokogiri::XML::CDATA:0x3ff87d82f248 "(function(){\nwindow.google=
.....this goes on for awhile

首选输出:

<Nokogiri::HTML::Document:0x3ff87d83d7d0 name="document" ...
  <Nokogiri::XML::Element:0x3ff87d83cf10 name="html"  ...
    <Nokogiri::XML::Attr:0x3ff87d83ce84 name="itemtype" value="http://schema.org/WebPage">] ...
    <Nokogiri::XML::Element:0x3ff87d82f964 name="title" ...
...

谢谢!

【问题讨论】:

  • 您的首选输出是“谢谢!” ???
  • 您需要为 Nokogiri::XML::NodeSet 和/或 Nokogiri::XML::Node 覆盖 Nokogiri 的 inspectto_s 方法。那时你可以让它看起来像你想要的任何东西。

标签: ruby nokogiri


【解决方案1】:

您可以使用Nokogiri::HTML::Document#to_html 漂亮地打印您的Nokogiri::HTML::Document 对象。

由于Nokogiri::HTML::Document 扩展Nokogiri::XML::Document 扩展Nokogiri::XML::Node,您还有其他几个serializing options 使用SaveOptions 输出到不同的格式。

这样做:

> document = Nokogiri::HTML(html.read)
> puts document.to_html

【讨论】:

  • 我说我不希望它是 html 格式的,因为我想查看 Nokogiri::HTML::Document 对象中有哪些节点、属性等。
【解决方案2】:

使用awesome_print gem:

$ gem install awesome_print
$ irb

require 'open-uri'
require 'nokogiri'
require 'awesome_print'

html = open("http://www.google.com")
document = Nokogiri::HTML(html.read)

ap document

与 Nokogiri 的 to_html 方法不同,它还提供缩进和语法高亮显示。它并不完美,但比默认打印输出更有用。

【讨论】:

  • 这不起作用。它仍然打印出一个 blob 并且不缩进。你试过这个吗?我还说我在我的问题描述下尝试了这个。
  • 在您使用示例输出编辑问题之前,我不明白您在追求什么。不,awesome_print 不会那样做。它确实为 HTML 提供了语法高亮,并为其他 Ruby 对象提供了缩进(虽然不是为 Nokogiri)。
猜你喜欢
  • 1970-01-01
  • 2011-09-29
  • 2010-12-26
  • 1970-01-01
  • 1970-01-01
  • 2013-08-25
  • 2020-09-16
  • 2021-08-19
  • 1970-01-01
相关资源
最近更新 更多