html – 带nokogiri的条带样式属性

我正在使用nokogiri擦除一个html页面,我想删除所有的样式属性.
我该如何实现? (我不使用rails,所以我不能使用它的消毒方法,我不想使用消毒宝石’因为我想黑名单删除不是白名单)
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>

解决方法

require 'nokogiri'

html = '<p class="post"><span style="font-size: x-large">bla bla</span></p>'
doc = Nokogiri::HTML(html)
doc.xpath('//@style').remove
puts doc.css('.post')
#=> <p class="post"><span>bla bla</span></p>

编辑显示您可以调用NodeSet#remove而不必使用.each(&:remove).

请注意,如果您有DocumentFragment而不是Document,则Nokogiri有a longstanding bug,其中从片段中进行搜索不会按预期的方式工作.解决方法是使用:

doc.xpath('@style|.//@style').remove

相关文章

vue阻止冒泡事件 阻止点击事件的执行 &lt;div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些