我正在使用open-uri来阅读声称在iso-8859-1中编码的网页.当我读取页面内容时,open-uri返回以ASCII-8BIT编码的字符串.
open("http://www.nigella.com/recipes/view/DEVILS-FOOD-CAKE-5310") {|f| p f.content_type, f.charset, f.read.encoding }
=> ["text/html", "iso-8859-1", #<Encoding:ASCII-8BIT>]
Run Code Online (Sandbox Code Playgroud)
我猜这是因为网页的字节(或字符)\ x92不是有效的iso-8859字符.http://en.wikipedia.org/wiki/ISO/IEC_8859-1.
我需要将网页存储为utf-8编码文件.关于如何处理编码不正确的网页的任何想法.我可以捕获异常并尝试猜测正确的编码,但这似乎很麻烦且容易出错.