Rails rake mechanize - Error - No such file to load -- Mechanize [closed]
EDIT: Forgot to include mechanize in the gem file
I get a error that says: No such file to load -- Mechanize'
when I run: rake import_stats
.
My statistik.rake
in lib/tasks
desc "Importer statistikker"
task :import_stats => :environment do
require 'Mechanize'
agent = WWW::Mechanize.new
agent.get("http://www.iqmedier.dk")
form = agent.page.forms.first
form.Username = 'username'
form.Password = 'password'
form.submit
agent.page.link_with(:href => "/Publisher/Stats").click
form = agent.page.forms.first
form.submit
@stats = agent.page.search('//tr')[-2].search('td').map{ |n| n.text }
Reklamer.create!(:virksomhed => 'Iqmedier', :dato => '(@stats[1])', :unik_klik => '(@stats[2])', :klik => '(@stats[3])', :unik_vis => '(@stats[4])', :vis => ('@stats[5]'), :leads => ('@stats[6]'), :ordre => '(@stats[6])', :cpc => '(@stats[7])', :earn => '(@stats[8])')
end
I am trying to create a row in the table Reklamer with the scraped data.
Best regards, A rails beginner
The task name is environment
, not enviroment
. There's a typo.
Change the line 2.
To invoke a rake task you do:
rake namespace:name_of_the_task
So in your case considering you dont have any namespace you should run:
rake import_stats
You might want a namespace, to do this you have to encapsulate the task within a namespace:
namespace :mechanize do
task :import_stats => :environment do
....
end
end
and then execute
rake mechanize:import_stats
精彩评论