rails LoadError: cannot load such file -- lib/scraper

rails LoadError: cannot load such file -- lib/scraper

在我的 rails 应用程序中,我有一个从另一个网页抓取数据的 rake 任务。我想将方法​​功能从 rake 任务中移出到 ruby class 或模块中。为此,我在 lib/tasks 中有 rake 任务,然后在 lib 中有 scraper.rb。在 rake 任务中,我有 require 'lib/scraper' 但这会引发错误。

这是我的佣金任务:

require "lib/scraper"
namespace :some_namespace do
    desc "A description"
    task :scrape_info => :environment do
        scraper = Scraper.new
        scraper.scrape_info
    end
end

还有 ruby 爬虫 class:

require 'mechanize'
class Scraper
    def scrape_info
        mechanize = Mechanize.new

        # Scrape players from fox sports
        url = "someurl"

        # do some other stuff
    end
end

您的代码应如下所示。

lib/scraper.rb:

require 'mechanize'

module Scraper
  class Scraper
    def scrape_info
    end
  end
end

lib/tasks/some_namespace.rake:

namespace :some_namespace do
  task some_task :environment do
    include Scraper
  end
end