【发布时间】: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 的
inspect或to_s方法。那时你可以让它看起来像你想要的任何东西。