前提・実現したいこと
下記のソースコードを実行しておりますが、method `crawl'が呼べませんというエラーが出てしまいます。
恐らく単純な話のような気がしますが、解決策がいまいち思い浮かばず、質問する次第です。。
発生している問題・エラーメッセージ
undefined method `crawl' for Download (call 'Download.connection' to establish a connection):Class => nil
ソースコード
class Download < ApplicationRecord def self.crawl_from_NASA utc_time = Time.now.gmtime year = utc_time.strftime("%Y") monthday = utc_time.strftime("%m%d") login_url = "https://login_url" get_url = "https://get_url.com/#{year}/#{monthday}" begin FileUtils.mkdir_p("tmp/storage/nc/stack") FileUtils.mkdir_p("tmp/storage/nc/#{year}") FileUtils.mkdir_p("tmp/storage/nc/#{year}/#{monthday}") crawl(login_url, get_url, year, monthday) puts "downloadに成功しました" rescue Exception => e if e.message == "Exception" puts "同じファイルがあったため、処理はキャンセルされました" else puts e.message end end end def crawl(login_url, get_url, year, monthday) agent = Mechanize.new agent.user_agent = 'Mac Mozilla' agent.pluggable_parser.default = Mechanize::Download agent.get(login_url) do |page| page.form_with(:action => '/login') do |form| formdata = { username: 'hoge', password: 'hoge', } form.field_with(:name => 'username').value = formdata[:username] form.field_with(:name => 'password').value = formdata[:password] end.submit end agent.get(get_url) do |page| array = page.search('table tr td a') file_names = [] array.each do |ele| str = ele.get_attribute('href') length = str.length file_names.push(str) if length >= 43 end file_names.sort! file_name = file_names.last download_url = get_url + "/" + file_name agent.get(download_url) do |download_page| file_array = [] entries = Dir::entries("tmp/storage/nc/#{year}/#{monthday}/") entries.each do |entry| length = entry.length file_array.push(entry) if length >= 43 end file_array.sort! last_file = file_array.last if file_name == last_file raise Exception else file_array.each do |file| File.rename("tmp/storage/nc/#{year}/#{monthday}/#{file}", "tmp/storage/nc/stack/#{file}") end download_page.save_as("tmp/storage/nc/#{year}/#{monthday}/#{file_name}") end end end end end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/17 01:25