Ruby 编码问题
我刚刚开始学习 Ruby,并且在编码方面遇到了问题;
require 'rubygems'
require 'mechanize'
agent = Mechanize.new
agent.get('myurl.....')
agent.page.search('#reciperesult a').each do |item|
c = Mechanize.new
c.get(item.attributes['href'])
puts c.page.search('#ingredients li').text
end
输出文本应该显示为 hønsekjøtt,但显示为 h├©nsekj├©tt。我正在使用 Ruby 1.8.7。有人能指出我正确的方向吗?
I'm just starting to learn Ruby and have a problem with encoding;
require 'rubygems'
require 'mechanize'
agent = Mechanize.new
agent.get('myurl.....')
agent.page.search('#reciperesult a').each do |item|
c = Mechanize.new
c.get(item.attributes['href'])
puts c.page.search('#ingredients li').text
end
The output text are shown like this h├©nsekj├©tt when it should have been shown like this hønsekjøtt. I'm using Ruby 1.8.7. Can anybody point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在哪里查看输出?是在控制台上吗?如果是这样,您运行的是 Windows XP 吗?我发现在我的 Windows 设置中,控制台(我在 Git+ 终端中进行开发)无法正确显示非 ASCII 字符。在我的 Mac 终端上,它们显示得很好。
如果您怀疑这是问题所在,请将输出写入文件,然后在文本编辑器中查看该文件。这应该会显示正确的输出。
Where are you viewing the output? Is it on the console? If so, are you running Windows XP? I have found that on my Windows setup the console (I do my development in the Git+ terminal) it does not show non-ascii characters correctly. On the terminal on my Mac they show fine.
If you suspect that this is the problem then write the output to a file and then view that file in a text editor. This should show you the correct output.
尝试
在脚本的开头添加:。
编辑:似乎其他人之前也遇到过编码问题。尝试遵循此处给出的建议
Try adding:
at the beginning of your script.
EDIT: seems someone else had an encoding problem before. Try to follow the advice given here