mamesaye mamesaye - 1 year ago 139
Ruby Question

Why does mechanize raise NoMethodError on my page2.links.each method chain?


mySpiderScript.rb:119:in ` block (3 levels) in <main>': undefined method `links' for #<Mechanize::Image:0x120a7e38> (NoMethodError)


agent2 =
page2 = agent2.get('')
page2.links.each do |link2| #line 119
name = link2.href.to_s

How do I fix this so that the script keeps running?


Here is what page2.body returns.

����JFIF���ICC_PROFILE�lcms0mntrRGB XYZ �*acspAPPL���-lcms
gTRC, bTRCL chrml$mluc
enUSsRGB built-inmluc
enUS2No copyright, use freelyXYZ ���-sf32
Y� J����*��������������XYZ o�8��XYZ $����XYZ b����paraff��

$.' ",#(7),01444'9=82<.342��C


Answer Source

From the comments:

[T]he body is not a valid mechanize object. How to skip it?

There are lot of ways to validate your object before trying to invoke a method on it. One way is to use the poorly-documented safe navigation operator (&.) introduced in Ruby 2.3.0. For example, using your existing code:

page2&.links&.each do |link2|

This will return nil if the object in page2 doesn't respond to #links, or the result of page2.links doesn't respond to #each. Program flow will then continue after the #each block formed by your page2&.links&.each method chain.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download