目前,我正在测试一个使用 Mechanize 和 ruby 1.9.2(Ubuntu Lucid 包)的 ruby 脚本。
第一行设置以下选项:
#!/usr/bin/env ruby -KU
一些 Mechanize 邮政编码触发以下异常:
/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'
Mechanize 在源程序中是这样使用的:
# 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() 对象和通过解析 File 对象的 Nokogiri 对象从磁盘读取的。
将数据打印到控制台不会显示明显的编码问题。
因此我的问题是:如何在使用 Mechanize 发布表单数据时解决此编码问题?
我是否必须告诉 Mechanize http POST 使用什么字符编码?
最佳答案
好吧,看起来像是 0.9.3 版本的 Mechanize 中的错误(可能是 issue 25 )。使用 the current git version of mechanize为我解决了发布问题。
但是 Mechanize 1.0 版本也应该包含修复程序。
关于ruby - 如何避免使用 ruby 1.9 和 Mechanize 表单提交的 Encoding::CompatibilityError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4070284/