Sebastian Delgado Sebastian Delgado - 3 months ago 7
Ruby Question

How to scrap only one specific ul with nokogiri?

I'm trying to get only the second ul element of this page https://en.wikipedia.org/wiki/September_8, which is all the events list.

this is what i have right now:

require 'nokogiri'
require 'open-uri'

doc = Nokogiri::HTML(open('https://en.wikipedia.org/wiki/September_8', {ssl_verify_mode: 0}))

doc.css('ul').each do |link|
puts link.content
end


the UL that i need is the second one present on the whole html document, how can i select only that one ?

Thanks for reading.

Answer

Try this

doc.css('ul')[1]

or if you want to use CSS selectors only

doc.css('ul:nth-of-type(2)')