Search code examples
htmlruby-on-railsrubyweb-scrapingnokogiri

How to scrape only one specific ul with Nokogiri?


I'm trying to get only the second ul element of 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 in the whole HTML document. How can I select only that one?


Solution

  • Try this

    doc.css('ul')[1]
    

    or if you want to use CSS selectors only

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