如何在Nokogiri Ruby中浏览HTML,所以&仍然&而不是&

pge*_*son 5 ruby nokogiri

我有一个标题doc.at('head/title').inner_html,&应该是&.

我的原始文件是:

<head><title>Foo & Bar</title></head>
Run Code Online (Sandbox Code Playgroud)

但是如下所示:

>> doc = Nokogiri::HTML.parse(file, nil, "UTF-8")
>> doc.at('head/title')
=> #<Nokogiri::XML::Element:0x..fdb851bea name="title" children=#<Nokogiri::XML::Text:0x..fdb850808 "Foo & Bar">>
>> doc.at('head/title').inner_html
=> "Foo &amp; Bar"
Run Code Online (Sandbox Code Playgroud)

我不想使用Iconv或CGI:

>> require 'cgi'
>> CGI.unescapeHTML(doc.at('head/title').inner_html)
=> "Foo & Bar"
Run Code Online (Sandbox Code Playgroud)

这是丑陋和不方便的.

Ben*_*mes 7

使用content而不是inner_html将内容作为纯文本而不是(X)HTML.

irb(main):011:0> doc.at('head/title').content
=> "Foo & Bar"
Run Code Online (Sandbox Code Playgroud)