-2

I am using Nokogiri to scrape data from a HTML document, but I'm running into the following error:

`block in <main>': undefined method `[]' for nil:NilClass (NoMethodError)

This is the code to reproduce the problem:

require 'rubygems'
require 'nokogiri'
require 'open-uri'

url = "http://www.somewebsite.com/somepage/some"
doc = Nokogiri::HTML(open(url))

puts doc.at_css("title").text
doc.css(".Info_listing").each do |x|
  puts x.at_css(".MoreInfo")[:href]
end

Does anyone know why I'm getting this error?

fivedigit
  • 18,464
  • 6
  • 54
  • 58
Anshul Kalra
  • 198
  • 3
  • 13

1 Answers1

2

at_css will return nil if there's no matching element.

If you want to get MoreInfo class element inside Info_listing-class element, you'd better to use following code:

doc.css(".Info_listing .MoreInfo").each do |x|
  puts x[:href]
end
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • and also can you tell me how to do it for the next pages? as the site has a pagination thing and i need the data till the last page. And this solution is only for a single page – Anshul Kalra Jan 31 '15 at 13:02
  • @AnshulKalra, The next page's url is: `http://www.indiacom.com/yellow-pages/ambulance-services/bangalore/?page=2`. Change the `page` number, repeat the same thing until there's no more `next` link. – falsetru Jan 31 '15 at 13:09
  • @AnshulKalra, Or find the `next` button and follow the link, repeat until there's no more `next` link. – falsetru Jan 31 '15 at 13:10