如何使Nokogiri透明地返回未编码的Html实体?
我如何使用Nokogiri使html实体(如德国UMLAUT)保持不变 即:如何使Nokogiri透明地返回未编码的Html实体?,html,ruby,nokogiri,entities,Html,Ruby,Nokogiri,Entities,我如何使用Nokogiri使html实体(如德国UMLAUT)保持不变 即: #这很好 node=Nokogiri::HTML.fragment(“ö;”) node.to_s#=>”ö " #这不是 node=Nokogiri::HTML.fragment(“ö”) node.to_s#=>”ö " #这就是我需要的 node=Nokogiri::HTML.fragment(“ö”) node.to_s#=>“ö” 我尝试过使用PARSE_选项和:save_选项来处理
#这很好
node=Nokogiri::HTML.fragment(“ö;”)
node.to_s#=>”ö "
#这不是
node=Nokogiri::HTML.fragment(“ö”)
node.to_s#=>”ö "
#这就是我需要的
node=Nokogiri::HTML.fragment(“ö”)
node.to_s#=>“ö”
我尝试过使用PARSE_选项和:save_选项来处理这两个问题,但无法找到让Nokogiri透明地像上面那样工作的方法
有什么建议吗?好的,我的问题已经由Aaron via/回答了:
需要“rubygems”
需要“nokogiri”
doc=Nokogiri::HTML::Document.new
doc.encoding='UTF-8'
#我们为1.4.2版本添加了上下文片段方法。这可能*
#在1.4.1中工作。如果你想搞乱1.4.2,从我的github构建,或者
#抓取我们的夜间构建之一:
#
#$sudo gem安装nokogiri-shttp://tenderlovemaking.com/
#
#另外,libxml2在处理UTF-8片段时存在编码错误,因此我
#建议您也升级到libxml2.7.7。
#
#希望有帮助!
放置文档片段(“ö”)
Tweet text:您可能需要等待1.4.2,但这是可能的。
# this is fine
node = Nokogiri::HTML.fragment('<p>ö</p>')
node.to_s # => '<p>ö</p>'
# this is not
node = Nokogiri::HTML.fragment('<p>ö</p>')
node.to_s # => '<p>ö</p>'
# this is what I need
node = Nokogiri::HTML.fragment('<p>ö</p>')
node.to_s # => '<p>ö</p>'
require 'rubygems'
require 'nokogiri'
doc = Nokogiri::HTML::Document.new
doc.encoding = 'UTF-8'
# We added a contextual fragment method for the 1.4.2 release. This *might*
# work in 1.4.1. If you want to mess with 1.4.2, build from my github, or
# grab one of our nightly builds:
#
# $ sudo gem install nokogiri -s http://tenderlovemaking.com/
#
# Also, libxml2 had a bug with encoding when handling UTF-8 fragments, so I
# suggest you also upgrade to libxml2 2.7.7.
#
# Hope that helps!
puts doc.fragment('<p>ö</p>')