2009-12-31 15 views
5

ho un titolo doc.at('head/title').inner_html che esce & e dovrebbe essere &.Come unescape HTML in Nokogiri Ruby, quindi e resta & e non &

Il documento originale è:

<head><title>Foo & Bar</title></head> 

ma esce come il seguente:

>> 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" 

Non voglio usare Iconv o CGI come:

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

è brutto e scomodo

risposta

7

Utilizzare content anziché inner_html per ottenere il contenuto come testo normale anziché (X) HTML.

irb(main):011:0> doc.at('head/title').content 
=> "Foo & Bar" 
Problemi correlati