How do I remove a node with Nokogiri?

不想你离开。 提交于 2019-11-27 12:54:34

问题


How can I remove <img> tags using Nokogiri?

I have the following code but it wont work:

# str = '<img src="canadascapital.gc.ca/data/2/rec_imgs/5005_Pepsi_H1NB.gif"/…; testt<a href="#">test</a>tfbu' 

f = Nokogiri::XML.fragment(str)
f.search('//img').each do |node| 
  node.remove
end
puts f

回答1:


have a try!

f = Nokogiri::XML.fragment(str)

f.search('.//img').remove
puts f



回答2:


I prefer CSS over XPath, as it's usually much more readable. Switching to CSS:

require 'nokogiri'

doc = Nokogiri::HTML('<html><body><img src="foo"><img src="bar"></body></html>')

After parsing the document looks like:

doc.to_html
# => "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body>\n<img src=\"foo\"><img src=\"bar\">\n</body></html>\n"

Removing the <img> tags:

doc.search('img').each do |src|
  src.remove
end

Results in:

doc.to_html
# => "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body></body></html>\n"


来源:https://stackoverflow.com/questions/1708504/how-do-i-remove-a-node-with-nokogiri

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!