如何在Rails中使用Nokogiri从URL获取HTML正文?

tar*_*djo 6 ruby-on-rails nokogiri

我想从URL解析body属性.

例如:

url = 'http://rca.yandex.com/?key=rca.1.1.20140120T051507Z.3db118ab435efdff.6c84331313b6b7d66abd191410f72e0e1c3c8795&url=http://endtimeheadlines.wordpress.com/2014/01/17/think-tank-extraordinary-crisis-needed-to-preserve-new-world-order/#comment-36708?utm_source=twitterfeed&utm_medium=facebook[&callback=http://64.191.99.245:3023/posts][&full=1]'
Run Code Online (Sandbox Code Playgroud)

当我尝试:

page = Nokogiri::HTML(html)
Run Code Online (Sandbox Code Playgroud)

我明白了:

#<Nokogiri::HTML::Document:0x52fd6d6 name="document" children=[#<Nokogiri::XML::DTD:0x52fd1f4 name="html">, #<Nokogiri::XML::Element:0x52fc6aa name="html" children=[#<Nokogiri::XML::Element:0x5301f56 name="body" children=[#<Nokogiri::XML::Element:0x53018d0 name="p" children=[#<Nokogiri::XML::Text:0x53015f6 "http://rca.yandex.com/?key=rca.1.1.20140120T051507Z.3db118ab435efdff.6c84331313b6b7d66abd191410f72e0e1c3c8795&url=http://endtimeheadlines.wordpress.com/2014/01/17/think-tank-extraordinary-crisis-needed-to-preserve-new-world-order/#comment-36708?utm_source=twitterfeed&utm_medium=facebook[&callback=http://64.191.99.245:3023/posts][&full=1]">]>]>]>]>
Run Code Online (Sandbox Code Playgroud)

如何获取此URL中的属性?

例如:page.css("div").我想从HTML中获取价值body.

the*_*Man 20

目前还不清楚你要做什么,但这可能会有所帮助:

require 'nokogiri'

html = '<html><head><title>foo</title><body><p>bar</p></body></html>'

doc = Nokogiri::HTML(html)
Run Code Online (Sandbox Code Playgroud)

使用时at,您将找到第一次出现的标记,这在HTML文档中是合理的,因为您应该只有一个<body>标记.

doc.at('body') # => #<Nokogiri::XML::Element:0x3ff194d24cd4 name="body" children=[#<Nokogiri::XML::Element:0x3ff194d24acc name="p" children=[#<Nokogiri::XML::Text:0x3ff194d248c4 "bar">]>]>
Run Code Online (Sandbox Code Playgroud)

如果您想要标记的子项,请使用它children来检索它们:

doc.at('body').children # => [#<Nokogiri::XML::Element:0x3ff194d24acc name="p" children=[#<Nokogiri::XML::Text:0x3ff194d248c4 "bar">]>]
Run Code Online (Sandbox Code Playgroud)

如果要将子节点作为HTML获取:

doc.at('body').children.to_html # => "<p>bar</p>"
doc.at('body').inner_html # => "<p>bar</p>"
Run Code Online (Sandbox Code Playgroud)

如果你想要body标签的文本内容:

doc.at('body').content # => "bar"
doc.at('body').text # => "bar"
Run Code Online (Sandbox Code Playgroud)

如果通过"属性",你真的是在attributes的的<body>标签本身:

require 'nokogiri'

html = '<html><head><title>foo</title><body on_load="do_something()"><p>bar</p></body></html>'

doc = Nokogiri::HTML(html)
doc.at('body').attributes # => {"on_load"=>#<Nokogiri::XML::Attr:0x3fdc3d923ca0 name="on_load" value="do_something()">}
doc.at('body')['on_load'] # => "do_something()"
Run Code Online (Sandbox Code Playgroud)

attributes返回一个哈希,这样你就可以直接访问你想要的任何东西.作为一种捷径,Nokogiri :: XML :: Node也理解[]为我们提供典型的Hash风格的访问权限.


vee*_*vee 0

您可以根据需要使用to_xml或或其他格式。to_html请参考Nokogiri文档了解其他格式选项。

page = Nokogiri::HTML(html)
page.to_xml
Run Code Online (Sandbox Code Playgroud)

div要在您的 中获取 的正文document,请使用:

divs = page.css('div') # returns either string or array depending upon the number of divs in your document.
divs.to_xml
Run Code Online (Sandbox Code Playgroud)