使用机械化提交表格时"Unsupported content-encoding: gzip,gzip"



我正在尝试使用 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

我假设机械化可以使用gzip内容编码,所以我不确定错误来自哪里。知道这里发生了什么吗?

红宝石 2.1.7,机械化 2.7.4。

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

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

没有更多的错误。

最新更新