【问题标题】:"Unsupported content-encoding: gzip,gzip" when submitting form with mechanize使用机械化提交表单时出现“不支持的内容编码:gzip、gzip”
【发布时间】:2016-02-16 22:51:55
【问题描述】:

我正在尝试使用 ruby​​ 中的 mechanize 库来抓取一些数据,我必须首先通过“条款和条件”页面。为此,我点击了“我同意”按钮。

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。

【问题讨论】:

    标签: ruby mechanize mechanize-ruby


    【解决方案1】:

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

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

    没有更多错误。

    【讨论】:

      猜你喜欢
      • 2018-02-27
      • 2015-01-17
      • 2011-09-30
      • 2015-03-11
      • 1970-01-01
      • 1970-01-01
      • 2019-05-25
      • 2017-03-16
      • 1970-01-01
      相关资源
      最近更新 更多