Search code examples
rubyattributesnokogiri

Nokogiri undefined method 'attribute' nilClass


This may seem like an obvious question, but I am very new. I am trying to scrape Rotten Tomatoes top 100 movie list for a simple CLI app. Everything goes fine until the hero.link line where I get a undefined method 'attribute' for nilClass. I'm just trying to get the value of the href of the title link. I've tried everything that I understand and more, but I just cannot figure out how to access what I want without using the attribute method.

However, when I get in with pry in the middle of the function, I'm able to type it in manually, it seems to work.

def new_with_rank
  self.get_top_page.css(".table tr").each do |e|
    hero = Top100::Movie.new
    hero.rank = e.css(".bold").text.delete!(".")
    binding.pry
    hero.rating = e.css(".tMeterScore").text.gsub!(/\u00A0/, "")
    hero.title = e.css(".unstyled").text
    hero.title.strip! #Don't know why I can't chain onto .text above
    hero.reviews = e.css("td.right.hidden-xs").text
    hero.link = e.css("td a").attribute("href").value
  end
  Top100::Movie.all.shift
  Top100::Movie.all
  binding.pry
end

Thank you for your help.


Solution

  • For future suggest such approach for debugging loops:

    def new_with_rank
      self.get_top_page.css(".table tr").each do |e|
        begin
          hero = Top100::Movie.new
          hero.rank = e.css(".bold").text.delete!(".")
          hero.rating = e.css(".tMeterScore").text.gsub!(/\u00A0/, "")
          hero.title = e.css(".unstyled").text
          hero.title.strip! #Don't know why I can't chain onto .text above
          hero.reviews = e.css("td.right.hidden-xs").text
          hero.link = e.css("td a").attribute("href").value
        rescue => error
          puts error
          puts error.backtrace
          binding.pry
        end
      end
      Top100::Movie.all.shift
      Top100::Movie.all
    end