自定义rake产生错误:rake aborted!不知道如何建立任务

时间:2014-08-28 13:55:13

标签: ruby-on-rails ruby web-scraping rake rvm

我正在尝试测试一个简单的rake任务(下面),但我不断收到错误消息:rake aborted! Don't know how to build task

LIB /任务/ 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

错误的跟踪是:

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

2 个答案:

答案 0 :(得分:1)

Nokogiri期待一个包含HTML的字符串。你给它一个HTTParty::Response对象。将Nokogiri @response.body作为参数而不仅仅是@response应该有效。

答案 1 :(得分:0)

您的任务名为scrape_bbc,但您尝试按名称scrape调用它。