使用以下代码(来自机械化站点,但在rake任务中)。。
namespace :ans do
task :grab => :environment do
a = Mechanize.new { |agent|
agent.user_agent_alias = 'Mac Safari'
}
begin
a.get('http://google.com/') do |page|
search_result = page.form_with(:name => 'f') do |search|
search.q = 'Hello world'
end.submit
search_result.links.each do |link|
puts link.text
end
end
end
end
end
我遇到编码错误。。
rake aborted!
"x8B" from ASCII-8BIT to UTF-8
这是在使用以下内容时:
Rails 3.1.3
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
Gems included by the bundle:
* actionmailer (3.1.3)
* actionpack (3.1.3)
* activemodel (3.1.3)
* activerecord (3.1.3)
* activeresource (3.1.3)
* activesupport (3.1.3)
* ansi (1.4.1)
* arel (2.2.1)
* builder (3.0.0)
* bundler (1.0.21)
* coffee-rails (3.1.1)
* coffee-script (2.2.0)
* coffee-script-source (1.2.0)
* domain_name (0.5.1)
* erubis (2.7.0)
* execjs (1.2.13)
* hike (1.2.1)
* i18n (0.6.0)
* jquery-rails (1.0.19)
* json (1.6.4)
* mail (2.3.0)
* mechanize (2.1)
* mime-types (1.17.2)
* multi_json (1.0.4)
* net-http-digest_auth (1.2)
* net-http-persistent (2.3.3)
* nokogiri (1.5.0)
* ntlm-http (0.1.1)
* pg (0.12.2)
* polyglot (0.3.3)
* rack (1.3.6)
* rack-cache (1.1)
* rack-mount (0.8.3)
* rack-ssl (1.3.2)
* rack-test (0.6.1)
* rails (3.1.3)
* railties (3.1.3)
* rake (0.9.2.2)
* rdoc (3.12)
* sass (3.1.12)
* sass-rails (3.1.5)
* sprockets (2.0.3)
* thor (0.14.6)
* tilt (1.3.3)
* treetop (1.4.10)
* turn (0.8.3)
* tzinfo (0.3.31)
* uglifier (1.2.1)
* unf (0.0.4)
* unf_ext (0.0.4)
* webrobots (0.0.12)
rake ans:grab --trace
** Invoke ans:grab (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute ans:grab
rake aborted!
"x8B" from ASCII-8BIT to UTF-8
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/mechanize-2.1/lib/mechanize/http/agent.rb:807:in `block in response_read'
/home/andy/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/protocol.rb:395:in `call_block'
/home/andy/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/protocol.rb:386:in `<<'
/home/andy/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/protocol.rb:90:in `read'
/home/andy/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:2776:in `read_chunked'
/home/andy/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:2750:in `read_body_0'
/home/andy/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:2710:in `read_body'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/mechanize-2.1/lib/mechanize/http/agent.rb:795:in `response_read'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/mechanize-2.1/lib/mechanize/http/agent.rb:267:in `block in fetch'
/home/andy/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:1322:in `block (2 levels) in transport_request'
/home/andy/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:2671:in `reading_body'
/home/andy/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:1321:in `block in transport_request'
/home/andy/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:1316:in `catch'
/home/andy/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:1316:in `transport_request'
/home/andy/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/net/http.rb:1293:in `request'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/net-http-persistent-2.3.3/lib/net/http/persistent.rb:621:in `request'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/mechanize-2.1/lib/mechanize/http/agent.rb:264:in `fetch'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/mechanize-2.1/lib/mechanize.rb:319:in `get'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/mechanize-2.1/lib/mechanize.rb:428:in `submit'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/mechanize-2.1/lib/mechanize/form.rb:178:in `submit'
/home/andy/foo/lib/tasks/fplJsonParse.rake:38:in `block (3 levels) in <top (required)>'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/mechanize-2.1/lib/mechanize.rb:321:in `get'
/home/andy/foo/lib/tasks/fplJsonParse.rake:37:in `block (2 levels) in <top (required)>'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/home/andy/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/home/andy/.rvm/gems/ruby-1.9.3-p0/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/home/andy/.rvm/gems/ruby-1.9.3-p0/bin/rake:19:in `load'
/home/andy/.rvm/gems/ruby-1.9.3-p0/bin/rake:19:in `<main>'
Tasks: TOP => ans:grab
只需安装以前的机械化版本:
gem install mechanize -v 2.0.1
或
gem 'mechanize', '2.0.1' and `bundle update`
我在2.1中遇到了同样的问题顺便说一句,目前正在讨论将github机械化的问题