I prefer CSS over XPath as it is usually more readable. Transition to CSS:
require 'nokogiri' doc = Nokogiri::HTML('<html><body><img src="foo"><img src="bar"></body></html>')
After parsing the document, it looks like this:
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 <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"
the Tin Man Sep 29 '13 at 4:03 2013-09-29 04:03
source share