【发布时间】:2013-02-15 06:03:58
【问题描述】:
我有这个代码:
# encoding: utf-8
require 'nokogiri'
s = "<a href='/path/to/file'>Café Verona</a>".encode('UTF-8')
puts "Original string: #{s}"
@doc = Nokogiri::HTML::DocumentFragment.parse(s)
links = @doc.css('a')
only_text = 'Café Verona'.encode('UTF-8')
puts "Replacement text: #{only_text}"
links.first.replace(only_text)
puts @doc.to_html
但是,输出是这样的:
Original string: <a href='/path/to/file'>Café Verona</a>
Replacement text: Café Verona
Café Verona
为什么@doc 中的文本编码错误?
我尝试使用和不使用encode('UTF-8') 或使用Document 而不是DocumentFragment,但这是同样的问题。
我正在使用 Nokogiri v1.5.6 和 Ruby 1.9.3p194。
【问题讨论】:
-
我无法使用 Ruby 2.0.0 或 1.9.3 重现它。顺便问一下,你的 Ruby 版本是什么?
-
你试过
puts @doc.to_html(:encoding => "UTF-8") -
@jonallard 这是 ruby 1.9.3p194(2012-04-20 修订版 35410)[x86_64-darwin11.4.0]
-
我试过 @PinnyM 但不起作用
标签: ruby encoding character-encoding nokogiri