Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3485030
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:51:30+00:00 2026-05-18T10:51:30+00:00

I need to remove an image with the give src img_src = http://domain/img.jpg @doc.xpath(//img[@src=’#{img_src}’])[0].remove

  • 0

I need to remove an image with the give src

img_src = "http://domain/img.jpg"
@doc.xpath("//img[@src='#{img_src}']")[0].remove

Doesn’t work. Tried it also like this

@doc.xpath("//img[@src='#{img_src}']") {|x| x.remove}

Doesn’t work either. Any ideas on what I’m doing wrong?

I got it. It was a stupid mistake. All your solutions were correct.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:51:31+00:00Added an answer on May 18, 2026 at 10:51 am

    Nokogiri has two different parser modes, one for XML and one for HTML. XML is strict and HTML is very relaxed because, well, HTML is not always well-behaved.

    doc = Nokogiri::XML('<xml><a>1</a></xml>')
    

    or

    doc = Nokogiri::HTML('<html><body>foo</body></html>')
    

    This is how I generally parse an HTML file:

    require 'nokogiri'
    require 'open-uri'
    
    doc = Nokogiri::HTML(open('http://www.example.com'))
    print doc.to_html
    # >> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    [...]
    

    To strip a tag you need to locate it first, then remove it. After we parse a HTML or XML document we’ll have a Nokogiri::HTML or Nokogiri::XML document respectively, and, at that point what we called “tags” are now called “nodes”. Nokogiri can find nodesets, which are nodes that match a search, or an individual node, which will be the first match from a search.

    This will search for the first node matching src="a.png" using a CSS accessor, which is generally easier/cleaner than XPath. Nokogiri understands both XPath and CSS very well, and there are some advantages to CSS mentioned on the website:

    require 'nokogiri'
    require 'open-uri'
    
    html = '<html><body><img src="a.png" /><img src="b.png" /></body></html>'
    
    doc = Nokogiri::HTML(html)
    doc.at('img[@src="a.png"]').remove
    print doc.to_html
    # >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    # >> <html><body><img src="b.png"></body></html>
    

    To locate all nodes matching the accessor you could replace doc.at('img[@src="a.png"]').remove with:

    doc.search('img[@src="a.png"]').each { |n| n.remove }
    

    The tutorials are worth reading too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a url like this http://example.com/blog/photos/photos/gallery/image/1 . And i need to remove the
I need to remove image tags from text, so both versions of the tag:
I have the following string... Overall: 21 (1,192,742<img src=/images/image/move_up.gif title=+7195865 alt=Up />)<br /> August:
I need to remove image subviews from a ScrollView and I tried removing from
I need to remove an image from sd card chosen by user. In my
I need to remove the tag image with regex. I'm working with C# .Net
I need to remove the white background from this image and make the background
I need to upload an image to a remote PHP server which expects the
I need to remove duplicate rows from a fairly large SQL Server table (i.e.
I need to remove temp files on Tomcat startup, the pass to a folder

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.