I’m building a scraper to pull stories from news sites. Here’s an example from the BBC news site:
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://www.bbc.co.uk/news/'))
doc.css('h2 a.story').each do |h2|
puts "#{h2.content} - http://www.bbc.co.uk#{h2["href"]}"
end
What do I need to do to return a specific number of results, rather than all of them?
I had a look at the Nokogiri documentation and it isn’t very explanatory.
This is not a Nokogiri problem, it’s a Ruby array problem.
Use
takefor the first three results:For the fourth to the eighth result use a Range: