我想使用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"]
但它不返回结束标记。
您可以将所有非自关闭的打开元素的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>"]
您可以设置自己的结束标记,如下所示:
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"]]
您实际上并没有像扫描那样进行解析
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>"]