Rails 5 个模型与 Nokogri gem return 无

Rails 5 model with Nokogri gem return nil

我的应用程序在 Rails 5 中有问题。我创建了 class scrape.rb 通过 Nokogiri gem 抓取 HTML 并可以将此数据保存在另一个模型中,但是当我在 rails 中创建新对象时控制台此 return nil 并且不丢弃任何值:

2.3.0 :018 > s = Scrape.new
 => #<Scrape:0x007fba68b79e98>
2.3.0 :019 > s.scrape_new_movie
 => nil
2.3.0 :020 >

这里是scrape.rb模特

 class Scrape
  attr_accessor :title, :vote, :image_url, :description,

  def scrape_new_movie
    begin
      doc = Nokogiri::HTML(open("https://zalukaj.com/zalukaj-film/26280/barbie_w_wiecie_gier_barbie_video_game_hero_2017_.html").read, nil, 'utf-8')
      doc.css('script').remove
      self.title = doc.css('#pw_title.about_movie_title').text
      v = doc.css('#success_vote').text
      self.vote = v.slice(2...5)
      self.image_url = doc.css('.about_movie img').attr('src').text
      self.description = doc.css('#pw_description.e_s3k').text
      return true
      rescue Exception => e
      self.failure = "Something went wrong with the scrape"
    end
  end

  def save_movie
    movie = Movie.new(
      title: self.title,
      vote: self.vote,
      image_url: self.image_url,
      description: self.description
    )
    movie.save
  end
end

替换方法:

  def scrape_new_movie
    begin
      doc = Nokogiri::HTML(open("https://zalukaj.com/zalukaj-film/26280/barbie_w_wiecie_gier_barbie_video_game_hero_2017_.html").read, nil, 'utf-8')
      doc.css('script').remove
      self.title = doc.css('#pw_title.about_movie_title').text
      v = doc.css('#success_vote').text
      self.vote = v.slice(2...5)
      self.image_url = doc.css('.about_movie img').attr('src').text
      self.description = doc.css('#pw_description.e_s3k').text
      return true
      rescue Exception => e
      self.failure = "Something went wrong with the scrape"
    end
  end

  def scrape_new_movie
    doc = Nokogiri::HTML(open("https://zalukaj.com/zalukaj-film/26280/barbie_w_wiecie_gier_barbie_video_game_hero_2017_.html").read, nil, 'utf-8')
    doc.css('script').remove
    self.title = doc.css('#pw_title.about_movie_title').text
    v = doc.css('#success_vote').text
    self.vote = v.slice(2...5)
    self.image_url = doc.css('.about_movie img').attr('src').text
    self.description = doc.css('#pw_description.e_s3k').text
    return true
  end

然后出现的任何错误都会冒出并以允许您调试问题所在的方式显示。

这是一个很好的例子,说明了为什么您永远不应该这样做 rescue Exception,因为这总是会使调试问题变得更加困难。参见:Why is it a bad style to `rescue Exception => e` in Ruby?

根据您的设置方式,无需调用 class 的字面名称。自己加就好了方法,不要调用新方法。如果您想对此进行调试,此脚本中还有很多错误,我也会提出异常消息。您还应该将 self.title = 更改为 @title = 或者如果您想保留 self.title 您需要添加 class 以继承自 self 并将 attr_accessor 放入 class.

class Scrape
  class << self
    attr_accessor :title, :vote, :image_url, :description, failure
  end 

  def self.scrape_new_movie
    begin
      doc = Nokogiri::HTML(open("https://zalukaj.com/zalukaj-film/26280/barbie_w_wiecie_gier_barbie_video_game_hero_2017_.html").read, nil, 'utf-8')
      doc.css('script').remove
      self.title = doc.css('#pw_title.about_movie_title').text
      v = doc.css('#success_vote').text
      self.vote = v.slice(2...5)
      self.image_url = doc.css('.about_movie img').attr('src').text
      self.description = doc.css('#pw_description.e_s3k').text
      return true
      rescue Exception => e
        raise e 
      end
    end

    def self.save_movie
      movie = Movie.new(
        title: self.title,
        vote: self.vote,
        image_url: self.image_url,
        description: self.description
      )
      movie.save
    end
 end      
 Scrape.scrape_new_movie

它返回 nil 的原因是因为您用逗号结束了 attr_accessor。变量 failure 也是未定义的,所以我假设你也想要一个 attr_accessor。

你应该改变

  attr_accessor :title, :vote, :image_url, :description,

  attr_accessor :title, :vote, :image_url, :description, :failure