Search code examples
rubyloopshyperlinkmechanizenomethoderror

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


Error

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

Code

agent2 = Mechanize.new
page2 = agent2.get('http://www.mywebsite.net')
  page2.links.each do |link2|     #line 119
    name = link2.href.to_s   

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

Update

Here is what page2.body returns.

����JFIF���ICC_PROFILE�lcms0mntrRGB XYZ �*acspAPPL���-lcms
                                                                                   desc8cprt@Nwtpt�chad�,rXYZ�bXYZ�gXYZ�rTRC
                                                                                                                                        gTRC, bTRCL chrml$mluc
                                                                                                                                                                  enUSsRGB built-inmluc
                                                                                                                                                                                          enUS2No copyright, use freelyXYZ ���-sf32
Y�                     J����*��������������XYZ o�8��XYZ $����XYZ b����paraff��
Y�raff��
Y�raff��
[chrm��T{L���&f\��





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


2!!22222222222222222222222222222222222222222222222222��"����������
                                                                                         ?����

Solution

  • 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.