undefined method `[]' for nil:NilClass while using Nokogiri - ruby

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?

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

Related

Figure out where a method was defined

If I follow the following part of this article:
Figure out where a method was defined
object = Object.new
puts object.method(:blank?).source_location
=> ["/gems/activesupport-5.0.0.beta1/lib/active_support/core_ext/object/blank.rb", 14]
I should be able to find the definition of the blank? method, however when I try this code within irb with ruby 2.0.0 I get this error message:
➜ ~ irb
irb(main):001:0> object = Object.new
=> #<Object:0x007fc84882f088>
irb(main):002:0> puts object.method(:blank?).source_location
NameError: undefined method `blank?' for class `Object'
from (irb):2:in `method'
from (irb):2
from /usr/bin/irb:12:in `<main>'
Did I miss anything?
Thank you.
.blank? method does not exist for a Object type. I know for sure it exists for a String method if I include the active_support lib
irb(main):001:0> String.new.method(:blank?).source_location
=> ["/home/xeon/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/activesupport-4.2.8/lib/active_support/core_ext/object/blank.rb", 116]
If you include activesupport-5.0.0.beta1 then it will work for you. (Looking at the source path of the article you have posted)

undefined method data_for for main:object error in Ruby

I wrote a small script that try to read data from a .yml file using DataMagic.
require 'watir-webdriver'
require 'data_magic'
DataMagic.yml_directory="/home/krishna/RUBY/"
DataMagic.load "testdata.yml"
testData = data_for("testdata.yml/Testcase_01")
puts "#{testData[:username]}"
when i execute this i am getting an error
hashdata.rb:7:in `<main>': undefined method `data_for' for main:Object (NoMethodError)
Tell me what i am missing. Thanks.
require 'watir-webdriver'
require 'data_magic'
include DataMagic
DataMagic.yml_directory="/home/krishna/RUBY/"
testData = data_for("testdata/Testcase_01")
puts "#{testData['username']}"

How do I use Mechanize to go through each link?

Am trying to go through a series of links with a css class title and click those links and then get the product title. But i keep getting the error undefined method each for #<Mechanize::Page::Link:0x007fbfe2524410> (NoMethodError)? I Don't understand what am doning wrong?
heres my code:
require 'mechanize'
file = File.new("outputscrape.txt", 'w')
agent = Mechanize.new { |agent|
agent.user_agent_alias = 'Windows Chrome'}
page = agent.get('http://www.amazon.com/s/ref=sr_nr_n_0?rh=n%3A283155%2Cn%3A%211000%2Cn%3A5%2Cn%3A15377001%2Cn%3A6133979011%2Cn%3A6133980011&bbn=6133979011&ie=UTF8&qid=1412193262&rnid=6133979011')
title_link = page.link_with(:dom_class => "title")
title_link.each do |link|
link.click
file.write(link.at('#productTitle').text.strip)
end
From the mechanize docs:
link_with(criteria)
Find a single link matching criteria.
You need to use:
links_with(criteria)
Find all links matching criteria.
The object mentioned in your error message, Page::Link:
undefined method each for #<Mechanize::Page::Link:0x007fbfe2524410>
(NoMethodError)
doesn't sound like more than one thing, does it? More than one thing would be more like Page::Links, or Page::Link::Group, or Page::LinkSet. You are doing the equivalent of:
10.each do |number|
puts number
end
However, numbers do not have an each() method, so that produces the error:
undefined method `each' for 10:Fixnum (NoMethodError)
Compare that to your error:
undefined method each for #<Mechanize::Page::Link:0x007fbfe2524410>
On the other hand an Array does have an each() method, so you can do this:
[10, 20, 30].each do |number|
puts number
end

Ruby. Crack gem. -- in `<main>': undefined method `[]' for nil:NilClass (NoMethodError) --

Dear stackoverflow community,
Beginner's question:
Why do I get the following error?
scraper_sample_2.rb:7:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)
>Exit code: 1
Here's my code (copied from a ruby's intro guide):
require "rubygems"
require "crack"
require "open-uri"
URL = "http://www.recovery.gov/pages/GetXmlData.aspx?data=recipientHomeMap"
Crack::XML.parse(open(URL).read)["totals"]["state"].each do |state|
puts ["id", "awarded", "received", "jobs"].map{|f| state[f]}.join(",")
end
Because Crack::XML.parse(open(URL).read)["totals"] is nil. Try to split the call you do on line 7 on several lines and debug each call separately. Maybe the answer you get is not what you expect.
Given the format of the xml returned from your source, Crack::XML.parse(open(URL).read)["totals"] will, as Ivaylo said, return nil. The format of the xml must have changed, as totals are now within /map/view.
To get the expected output, change your code to:
Crack::XML.parse(open(URL).read)["map"]["view"]["totals"]["state"].each do |state|
puts ["id", "awarded", "received", "jobs"].map{|f| state[f]}.join(",")
end

Undefined method `name' for nil:NilClass (NoMethodError) when running script

When I run the following script to retrieve the first page of google results
#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://www.google.co.uk/search?q=stackoverflow'))
doc.css('div.vsc').each do |element|
puts element.at_css("h3.r a.l").content
end
I get a undefined methodcontent' for nil:NilClass (NoMethodError)`
How could I solve that? Or at least how could avoid it showing when executing?
As Dave Newton already pointed out in his comment, the result of at_css("h3.r a.l") is nil in your case. Neither the NilClass nor the object nil have a method content.
Workaround:
doc.css('div.vsc').each do |element|
next unless elem = element.at_css("h3.r a.l")
puts elem.content
end

Resources