我用nokogiri取消了一个html页面,我想剥去所有样式属性。
我该如何做到这一点? (我不使用轨道,所以我不能用它的sanitize方法,我不希望使用的sanitize宝石“因为我想黑名单中删除不白名单)nokogiri带状样式属性
html = open(url)
doc = Nokogiri::HTML(html.read)
doc.css('.post').each do |post|
puts post.to_s
end
=> <p><span style="font-size: x-large">bla bla <a href="http://torrentfreak.com/netflix-is-killing-bittorrent-in-the-us-110427/">statistica</a> blabla</span></p>
我希望它是
=> <p><span>bla bla <a href="http://torrentfreak.com/netflix-is-killing-bittorrent-in-the-us-110427/">statistica</a> blabla</span></p>
哇。那很简单!我喜欢它。谢谢! – keepitterron 2011-05-25 08:14:16
使用'doc.xpath('.//@style')。remove'从所有节点中删除所有内联样式,请注意下面的@bricker提到的'.'。链'.to_s'获取生成的html字符串。 – 2014-03-16 01:08:49
更正:不要链接它,而是使用'description.to_s'来获得生成的html字符串。如果您不想使用'DOCTYPE',则应该使用'Nokogiri :: HTML.fragment'方法,请参阅http://stackoverflow.com/questions/4723344/how-to-prevent-nokogiri-from-adding- doctype-tags – 2014-03-16 01:17:15