如何使用 ruby 1.9 避免 Encoding::CompatibilityError 并机械化表单提交?
目前,我正在测试一个 ruby 脚本,该脚本使用 mechanize 和 ruby 1.9.2(Ubuntu Lucid 软件包)。
第一行设置以下选项:
#!/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() 对象和 Nokogiri- 从磁盘读取解析 File 对象的对象。
将数据打印到控制台没有显示明显的编码问题。
因此我的问题是:使用 mechanize 发布表单数据时如何解决此编码问题?
我是否必须告诉 mechanize http POST 使用什么字符编码?
Currently, I am testing a ruby script that uses mechanize with ruby 1.9.2 (Ubuntu Lucid packages).
The first line sets following options:
#!/usr/bin/env ruby -KU
Some mechanize post code triggers following exception:
/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 is used like that in the source program:
# f is a Mechanize::Page object
final = f.form('final')
f.form('final') { |x|
f.form('final') { |x|
x.user == user
# ...
}
a = final.submit
The data that is filled in (e.g. user) is read from disk via File.new() objects and via Nokogiri-objects that parse on File objects.
Printing the data to console does not show obvious enconding problems.
Thus my question: How to I fix this encoding issue when posting form data using mechanize?
Do I have to tell mechanize what what character encoding to use for http POSTs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,看起来像是 mechanize 0.9.3 版本中的一个错误(可能是 issue 25< /a>)。使用 当前 git 版本的 mechanize 为我解决了发帖问题。
但 mechanize 1.0 版本也应该包含修复程序。
Well, looks like a bug in the 0.9.3 version of mechanize (probably issue 25). Using the current git version of mechanize fixes the posting issues for me.
But a mechanize 1.0 version should include the fix as well.