如何使Nokogiri不转换太空 [英] how to make Nokogiri not to convert   to space

查看:68
本文介绍了如何使Nokogiri不转换太空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我获取了一个类似html的片段

i fetch one html fragment like

"<li>市&nbsp;场&nbsp;价"

包含"&nbsp;",但是在调用Nokogiri NodeSet的to_s之后,它将变为

which contains "&nbsp;", but after calling to_s of Nokogiri NodeSet, it becomes

"<li>市 场 价"

,我想保留原始的html片段,并尝试为to_s方法设置:save_with option,但是失败了.

, i want to keep the original html fragment, and tried to set :save_with option for to_s method, but failed.

有人可以遇到相同的问题并给我帮助吗?预先谢谢你.

can someone encounter the same problem and give me help? thank you in advance.

推荐答案

我遇到了类似的情况,但我提出的建议有点破解,但似乎效果很好.

I encountered a similar situation, and what I came up was a bit of a hack, but it seems to work well.

nbsp = Nokogiri::HTML("&nbsp;").text
text.gsub(nbsp, " ")

就我而言,我希望nbsp成为常规空间.我认为在您的情况下,您希望将它们返回到& nbsp;",因此您可以执行以下操作:

In my case, I wanted the nbsp to be a regular space. I think in your case, you want them to be returned to a "&nbsp;", so you could do something like:

nbsp = Nokogiri::HTML("&nbsp;").text
html.gsub(nbsp, "&nbsp;")

这篇关于如何使Nokogiri不转换太空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆