Nokogiri获得所有HTML节点

时间:2016-08-05 13:39:14

标签: html ruby nokogiri

我想使用Nokogiri从HTML文档中获取所有节点。 示例HTML输入字符串:

<html>
  <body>
    <h1>Test</h1>
    <p>test <strong> Jojo </strong></p>
  </body>
</html>

预期产出:

['<html>','<body>','<h1>','</h1>','<p>','<strong>','</strong>','</p>','</body>','</html>']

关闭标签和正确的顺序很重要!

我已经尝试过这段代码了:

require 'nokogiri'
string_page = "<html><body><h1>Header1</h1></body></html>"
doc = Nokogiri::HTML(string_page)
doc.search('*').map(&:name)
# => ["html", "body", "h1"]

但它没有返回结束标签。

3 个答案:

答案 0 :(得分:4)

您可以将OuterXml拆分为非自动关闭的所有开放元素的InnerXml,存储相应的关闭元素(如果有)以检索它,并使用Nokogiri读取器解析文档,以根据文档中的顺序构建列表。

它要求您的文档是有效的XML片段,因为它使用的是XML解析器而不是HTML解析器。

require 'nokogiri'
[ "<html><body><h1>Header1</h1></body></html>",
"<html><body><div><h1>Title</h1><hr /></div><div><p>Lorem Ipsum<br />sit <span class=\"style\">d</span>olor</p></div></body></html>", <<END
<html>
  <body>
      <h1>Test</h1>
      <p>test <strong> Jojo </strong></p>
  </body>
</html>
END
].each { |string_page|
  elem_all = Array.new
  elem_ends = Hash.new
  reader = Nokogiri::XML::Reader(string_page)
  reader.each { |node|
    if node.node_type.eql?(1)
      if node.self_closing?
        elem_all << node.outer_xml
      else
        elem_tags = node.outer_xml.split(node.inner_xml)
        elem_all << elem_tags.first
        elem_ends[node.local_name] = elem_tags[1] unless elem_tags.one?
      end
    end
    elem_all << elem_ends[node.local_name] if node.node_type.eql?(15) and elem_ends.has_key?(node.local_name)
  }

  puts string_page
  puts elem_all.to_s
  puts
}

输出:

<html><body><h1>Header1</h1></body></html>
["<html>", "<body>", "<h1>", "</h1>", "</body>", "</html>"]

<html><body><div><h1>Title</h1><hr /></div><div><p>Lorem Ipsum<br />sit <span class="style">d</span>olor</p></div></body></html>
["<html>", "<body>", "<div>", "<h1>", "</h1>", "<hr/>", "</div>", "<div>", "<p>", "<br/>", "<span class=\"style\">", "</span>", "</p>", "</div>", "</body>", "</html>"]

<html>
  <body>
      <h1>Test</h1>
      <p>test <strong> Jojo </strong></p>
  </body>
</html>
["<html>", "<body>", "<h1>", "</h1>", "<p>", "<strong>", "</strong>", "</p>", "</body>", "</html>"]

答案 1 :(得分:0)

您可以设置自己的结束标记,如下所示:

doc = Nokogiri::HTML(<<EOT)
<html>
  <body>
    <h1>Test</h1>
    <p>test <strong> Jojo </strong></p>
  </body>
</html>
EOT

p doc.search('*').map{|m| [m.name, "/#{m.name}"]}

# => [["html", "/html"], ["body", "/body"], ["h1", "/h1"], ["p", "/p"], ["strong", "/strong"]]

答案 2 :(得分:0)

您真正解析的不如扫描:

str = <<EOF
<html>
  <body>
    <h1>Test</h1>
    <p>test <strong> Jojo </strong></p>
  </body>
</html>
EOF

str.scan /<.*?>/
#=> ["<html>", "<body>", "<h1>", "</h1>", "<p>", "<strong>", "</strong>", "</p>", "</body>", "</html>"]