I am extracting a page with Nokogiri which returns a Nokogiri::XML::NodeSet. From this, I would like to extract only the text from even nodes.
doc.search("h2 a").class #=> Nokogiri::XML::NodeSet
doc.search("h2 a").count #=> returns 148
I am interested in 0,2,4,8, etc.:
doc.search("h2 a")[0].text #=> the one I wanted.
doc.search("h2 a")[2].text #=> the one I wanted.
Try the below :
doc.search("h2 a").map.with_index(0) do |i,nd|
i.text if nd.even?
end.compact
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments