ruby - Nokogiri group flat structure -


i have html structure like:

<div class='content'>  <h2>title</h2>  <p>some content title</p>  <h2>another title</h2>   <p>content title</p>  <p>some more content title</p>  <h2>third</h2>  <p>third content</p> </div> 

i trying write code output:

title  - content title title  - content title  - more content title third  - third content 

i've never used nokogiri until 5 minutes ago, can come far is:

content = doc.at_css('.content') content.css('h2').each |node|   puts node.text end content.css('p').each |node|   puts " - "   puts node.text end 

this doesn't group pieces together. how can achieve required grouping nokogiri?

there many ways it, here's one:

doc.at_css('.content').element_children.each |node|   puts(node.name == "h2" ? node.text : " - #{node.text}")   end 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -