Nokogiri未定义的方法‘Attribute’nilClass

vwhgwdsa  于 2022-10-15  发布在  Ruby
关注(0)|答案(1)|浏览(113)

这似乎是一个显而易见的问题,但我是个新手。我正在尝试从Rotten Tomatoes top 100 movie列表中获取一个简单的CLI应用程序。一切都很顺利,直到Herer.link行,在那里我得到了nilClass的一个未定义的方法‘属性’。我只是想知道标题链接的href的值。我已经尝试了我理解的所有方法,甚至更多,但我就是想不出如何在不使用属性方法的情况下访问我想要的东西。
然而,当我在函数中间使用Pry时,我可以手动键入它,它似乎起作用了。

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

谢谢你的帮助。

mspsb9vt

mspsb9vt1#

对于将来,建议使用以下方法来调试循环:

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

相关问题