[英]parse html tree with nested loops using nokogiri
Hi I'm new to nokogiri and trying to parse an HTML document with a varied tree structure. 嗨,我是nokogiri的新手,正在尝试解析具有不同树结构的HTML文档。 Any suggestions on how to go about parsing it would be great. 关于如何进行解析的任何建议都很好。 I'd like to capture all the text on this page. 我想捕获此页面上的所有文本。
<div class = "main"> Title</div>
<div class = "subTopic">
<span = "highlight">Sub Topic</span>Stuff
</div>
<div class = "main"> Another Title</div>
<div class = "subTopic">
<span class = "highlight">Sub Topic Title I</span>Stuff<br>
<span class = "highlight">Sub Topic Title II</span>Stuff<br>
<span class = "highlight">Sub Topic Title III</span>Stuff<br>
</div>
I tried this but it just puts out each full array and I'm not even sure how to get to the "Stuff" part. 我试过了,但是它只是把每个完整的数组都取出来了,我什至不知道如何进入“ Stuff”部分。
content = Nokogiri::HTML(open(@url))
content.css('div.main').each do |m|
puts m .text
content.css('div.subTopic').each do |s|
puts s.text
content.css('span.highlight').each do |h|
puts h.text
end
end
end
Help will be appreciated. 帮助将不胜感激。
Something like that would parse your give document structure: 这样的事情将解析您的给定文档结构:
Data 数据
<div class="main"> Title</div>
<div class="subTopic">
<span class="highlight">Sub Topic</span>Stuff
</div>
<div class = "main"> Another Title</div>
<div class = "subTopic">
<span class = "highlight">Sub Topic Title I</span>Stuff<br>
<span class = "highlight">Sub Topic Title II</span>Stuff<br>
<span class = "highlight">Sub Topic Title III</span>Stuff<br>
</div>
Code: 码:
require 'nokogiri'
require 'pp'
content = Nokogiri::HTML(File.read('text.txt'));
topics = content.css('div.main').map do |m|
topic={}
topic['title'] = m.text.strip
topic['highlights'] = m.xpath('following-sibling::div[@class=\'subTopic\'][1]').css('span.highlight').map do |h|
topic_highlight = {}
topic_highlight['highlight'] = h.text.strip
topic_highlight['text'] = h.xpath('following-sibling::text()[1]').text.strip
topic_highlight
end
topic
end
pp topics
Will print: 将打印:
[{"title"=>"Title",
"highlights"=>[{"highlight"=>"Sub Topic", "text"=>"Stuff"}]},
{"title"=>"Another Title",
"highlights"=>
[{"highlight"=>"Sub Topic Title I", "text"=>"Stuff"},
{"highlight"=>"Sub Topic Title II", "text"=>"Stuff"},
{"highlight"=>"Sub Topic Title III", "text"=>"Stuff"}]}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.