Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove a node with Nokogiri?

Tags:

ruby

nokogiri

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 
like image 790
all jazz Avatar asked Nov 10 '09 14:11

all jazz


People also ask

How does Nokogiri work?

Nokogiri makes an attempt to determine whether a CSS or XPath selector is being passed in. It's possible to create a selector that fools at or search so occasionally it will misunderstand, which is why we have the more specific versions of the methods.

What is Nokogiri gem used for?

The Nokogiri gem is an incredible open-source tool that parses HTML and XML data. It is one of the most widely used gems available, and it can really take your Ruby app to another level for data with its ability to help you intuitively scrape websites.


1 Answers

have a try!

f = Nokogiri::XML.fragment(str)  f.search('.//img').remove puts f 
like image 60
xds2000 Avatar answered Oct 03 '22 22:10

xds2000