代码之家  ›  专栏  ›  技术社区  ›  maxschlepzig

如何避免编码::CompatibilityError与Ruby1.9和MechanicFormSubmit?

  •  1
  • maxschlepzig  · 技术社区  · 14 年前

    目前,我正在测试一个Ruby脚本,它使用Ruby1.9.2(UbuntuLucid包)的机械化。

    第一行设置以下选项:

    #!/usr/bin/env ruby -KU
    

    一些机械化的后代码触发以下异常:

    /usr/lib/ruby/1.9.1/www/mechanize/form.rb:222:in `join': incompatible character encodings: ISO-8859-1 and ASCII-8BIT (Encoding::CompatibilityError)
        from /usr/lib/ruby/1.9.1/www/mechanize/form.rb:222:in `request_data'
        from /usr/lib/ruby/1.9.1/www/mechanize.rb:401:in `post_form'
        from /usr/lib/ruby/1.9.1/www/mechanize.rb:344:in `submit'
        from /usr/lib/ruby/1.9.1/www/mechanize/form.rb:130:in `submit'
    

    在源程序中使用机械化:

    # f is a Mechanize::Page object
    final = f.form('final')
      f.form('final') { |x|
    f.form('final') { |x|
      x.user == user
      # ... 
    }
    a = final.submit
    

    填充的数据(例如用户)通过file.new()对象和对文件对象进行解析的nokogiri对象从磁盘读取。

    将数据打印到控制台没有显示明显的编码问题。

    因此,我的问题是:在使用机械化发布表单数据时,如何解决这个编码问题?

    我是否必须告诉Mechanical要为HTTP文章使用什么字符编码?

    1 回复  |  直到 14 年前
        1
  •  1
  •   maxschlepzig    14 年前

    嗯,看起来像0.9.3版的机械化(可能是 issue 25 )使用 the current git version of mechanize 为我修复了发布问题。

    但是机械化的1.0版本也应该包括修复。