Nokogiri undefined method 'attribute' nilClass

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

However, when I hit with pry in the middle of the function, I can enter it manually, it works.

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.

+3


source to share


1 answer


In the future, suggest this approach for debug 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

      

+2


source







All Articles