Search code examples
ruby-on-railsrubyweb-scrapingrakervm

Custom rake produces error: rake aborted! Don't know how to build task


I'm trying to test a simple rake task (below) but i keep getting the error message: rake aborted! Don't know how to build task.

lib/tasks/scrape.rake

task scrape_bbc: :environment do

  @request = HTTParty.get("http://feeds.bbci.co.uk/news/rss.xml")

  @html = Nokogiri::HTML(@request)
  @html.css("#item ul li")[0..4].each do |link|

   puts link.text

  end

end

The trace for the error is:

rake aborted!
NoMethodError: undefined method `match' for #<HTTParty::Response:0x007fbcc4fb6430>
/Users/James/.rvm/gems/ruby-2.1.2@rails414/gems/httparty-0.13.1/lib/httparty/response.rb:66:in `method_missing'
/Users/James/.rvm/gems/ruby-2.1.2@rails414/gems/nokogiri-1.6.3.1/lib/nokogiri/html/document.rb:255:in `detect_encoding'
/Users/James/.rvm/gems/ruby-2.1.2@rails414/gems/nokogiri-1.6.3.1/lib/nokogiri/html/document.rb:201:in `parse'
/Users/James/.rvm/gems/ruby-2.1.2@rails414/gems/nokogiri-1.6.3.1/lib/nokogiri/html.rb:15:in `HTML'
/Users/James/Projects/colate/lib/tasks/scrape.rake:5:in `block in <top (required)>'
/Users/James/.rvm/gems/ruby-2.1.2@rails414/bin/ruby_executable_hooks:15:in `eval'
/Users/James/.rvm/gems/ruby-2.1.2@rails414/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => scrape_bbc

Solution

  • Nokogiri is expecting a string containing HTML. You've given it an HTTParty::Response object. Giving Nokogiri @response.body as an argument instead of just @response should work.