I am attempting to scrape data from a website. I am running rake from index.html.erb
<% Article.run_rake("fetch_games") %>
I have it defined in a .rb file. Here is article.rb
require 'rake'
require 'rubygems'
#load './lib/tasks/article_task.rake'
class Article < ActiveRecord::Base
def self.run_rake(fetch_games)
Rails.root + "lib/tasks/article_task.rake"
Rake::Task["fetch_games"].invoke
end
end
And here is the rakefile itself: article_task.rake
desc "Fetch Games"
task :fetch_games => :environment do
require 'nokogiri'
require 'open-uri'
url = "http://espn.go.com/nba/schedule"
data = Nokogiri::HTML(open(url))
games = data.css('.responsive-table-wrap')
games.each do |game|
#check for a listing
if !game.at_css("caption").nil?
#Date
puts game.at_css("caption").text
else
puts "No Listing"
end
#check for the team name
if !game.at_css(".team-name").nil?
#Team name
puts game.at_css(".team-name").text
else
puts "No Games Scheduled"
end
#empty
puts ""
end
end
When I run this from the terminal it pulls what I need. But when I try to run it through rails server it gives me this error:
What am I doing wrong? New to ruby/rails btw