I'm using Nokogiri to parse a webpage.
I'm able to succefully get the 1st bookTitle class element, but I'm trying to get all of the titles in an array.
But I'm struggling to get all the other elements. What's the best way to get them all?
doc = Nokogiri::HTML(URI.open("https://www.goodreads.com/search?utf8=✓&q=barack obama&search_type=books&search[field]=author"))
puts doc.at_css('.bookTitle').content
# returns Dreams from My Father: A Story of Race and Inheritance
puts doc.xpath('//*[@]').size;
# returns 20, which is correct
CodePudding user response:
at_css
(or just at
) returns first matched element (Nokogiri::XML::Element
)
You need css
method. It returns collection of matched elements (Nokogiri::XML::NodeSet
)
doc.css('.bookTitle')
Probably you need to map through such collection
doc.css('.bookTitle').lazy.map(&:text).map(&:strip).to_a