gpt4 book ai didi

ruby - 如何在 Nokogiri Ruby 中取消转义 HTML,所以 & 仍然是 & 而不是 &

转载 作者:数据小太阳 更新时间:2023-10-29 07:14:54 24 4
gpt4 key购买 nike

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

我的原始文档是:

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

但结果如下:

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

我不想像这样使用 Iconv 或 CGI:

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

这很丑陋而且不方便。

最佳答案

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

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

关于ruby - 如何在 Nokogiri Ruby 中取消转义 HTML,所以 & 仍然是 & 而不是 &,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1985347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com