Hpricot - UTF-8 issues

拟墨画扇 提交于 2019-12-11 05:45:53

问题


I get the following error when running the code below:

invalid byte sequence in UTF-8 (ArgumentError)

The code:

require 'hpricot'
require 'open-uri'

doc = open('http://www.amazon.co.jp/') {|f| Hpricot(f.read) }
puts doc.to_html

Hpricot cannot parse the Japanese content. Any suggestions on fixing this issue?


回答1:


The site doesn't seem to be using UTF-8: <meta http-equiv="content-type" content="text/html; charset=Shift_JIS" />.

Try this instead:

open('http://www.amazon.co.jp/') {|f| Hpricot(f.read.encode("UTF-8")) }


来源:https://stackoverflow.com/questions/11016328/hpricot-utf-8-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!