def detect_encode
require 'hpricot'
require 'open-uri'
@doc = open("http://www.baidu.com") { |f| Hpricot(f) }
guess_res = NKF.guess(@doc.to_s)
case guess_res
when NKF::SJIS
@encode = "Shift_JIS"
when NKF::EUC
@encode = "EUC"
when NKF::JIS
@encode = "JIS"
when NKF::UTF8
@encode = "UTF-8"
end
pp @encode # 取得编码
end
编码转换
def self.convert_char_encoding(encoding, data)
begin
case encoding
when 'jis'
return Kconv.tojis(data)
when 'Shfit-JIS'
return Kconv.tosjis(data)
when 'EUC-JP'
return Kconv.toeuc(data)
when 'UTF-8'
return Kconv.toutf8(data)
else
exit(1)
end
rescue SystemExit => e
p 'set invalid charcter encoding or nil.'
exit(0)
end
end