"不支持的内容编码:gzip,gzip"使用mechanize

时间:2016-02-16 22:51:55

标签: ruby mechanize mechanize-ruby

我试图使用ruby中的机械化库来搜索一些数据,我必须首先通过"条款和条件"页。为此,我点击了一个"我同意"按钮。

require 'mechanize'

agent = Mechanize.new
agent.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
agent.get('https://apply.hobartcity.com.au/Common/Common/terms.aspx')

form = agent.page.form_with(:id => 'aspnetForm')
button = form.button_with(:name => 'ctl00$ctMain$BtnAgree')
page = form.submit(button)

但是当我运行上面的代码时,我在表单提交步骤中得到了这个错误:

  

未捕获的异常:不支持的内容编码:gzip,gzip

当我使用浏览器访问第二页时,响应标题为

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
X-UA-Compatible: IE=9,10,11
Date: Tue, 16 Feb 2016 22:44:27 GMT
Cteonnt-Length: 16529
Content-Encoding: gzip
Content-Length: 5436

我认为mechanize可以使用gzip内容编码,因此我不确定错误的来源。任何想法在这里发生了什么?

Ruby 2.1.7,机械化2.7.4。

1 个答案:

答案 0 :(得分:0)

我没有弄清楚问题的实际原因是什么,但我能够通过覆盖内容编码来解决这个问题:

agent.content_encoding_hooks << lambda { |httpagent, uri, response, body_io|
  response['Content-Encoding'] = 'gzip'
}
agent.submit(form, button)

不再有错误。