Ruby 用 mechanize 时报:UndefinedConversionError

changwu · November 15, 2013 · Last by changwu replied at November 15, 2013 · 4960 hits

用 mechanzie 时: @page = agent.post(detail_url,data) @page.encoding = 'utf-8' File.open("./test.log","w:utf-8") do |file| file.write @page.body end
报错啊:

网页的编码肯定是 utf-8 啦。 同个网站其他内容都可以,就一些不行。 是什么问题呢?换其他编码都是乱码。

File.open("./test.log", 'w:ASCII-8BIT:utf-8') do |file|这样?

#1 楼 @ywjno 谢谢,这样可以了。

可是保存到 mongodb,还是报这样的错误。

page.body.force_encoding("ASCII-8BIT").force_encoding("utf-8") 这样就 ok 了。

You need to Sign in before reply, if you don't have an account, please Sign up first.