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 4627572
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:35:11+00:00 2026-05-22T03:35:11+00:00

When I try this: item.css(a).each do |a| if !a.starts_with? ‘http://’ a.replace a.content end end

  • 0

When I try this:

item.css("a").each do |a|
  if !a.starts_with? 'http://'
     a.replace a.content
  end
end

I get:

NoMethodError: undefined method 'starts_with?' for #<Nokogiri::XML::Element:0x1b48a60> 

EDIT:

Sure there is a cleaner way, but this seems to be working.

item.css("a").each do |a|
  unless a["href"].blank?
    if !a["href"].starts_with? 'http://' 
      a.replace a.content
    end
  end
end
  • 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-22T03:35:12+00:00Added an answer on May 22, 2026 at 3:35 am

    The problem is you’re trying to use the starts_with method on an object that doesn’t implement it.

    item.css("a").each do |a|
    

    will return XML nodes in a. Those belong to Nokogiri. What you want to do is convert the node to text, but only the part you want to check, which, because it’s a parameter of the node, can be accessed like this:

    a['href']
    

    So, you want to use something like this:

    item.css("a").each do |a|
      if !(a.starts_with?['href']('http://'))
         a.replace(a.content)
      end
    end
    

    The downside to this is you have to walk through every <a> tag in the document, which can be slow on a big page with lots of links.

    An alternate way to go about it is to use XPath’s starts-with function:

    require 'nokogiri'
    
    item = Nokogiri::HTML('<a href="doesnt_start_with">foo</a><a href="http://bar">bar</a>')
    puts item.to_html
    

    which outputs:

    >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    >> <html><body>
    >> <a href="doesnt_start_with">foo</a><a href="http://bar">bar</a>
    >> </body></html>
    

    Here’s how to do it using XPath:

    item.search('//a[not(starts-with(@href, "http://"))]').each do |a|
      a.replace(a.content)
    end
    puts item.to_html
    

    Which outputs:

    >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    >> <html><body>foo<a href="http://bar">bar</a>
    >> </body></html>
    

    The advantage to using XPath to find the nodes is it all runs in compiled C, rather than letting Ruby do it.

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

Sidebar

Related Questions

When I try this code: class MyStuff: def average(a, b, c): # Get the
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
When I try this with a generic class where this.value is T: if (this.value.GetType()
When I try this code: a, b, c = (1, 2, 3) def test():
Using something like this: try: # Something... except Exception as excep: logger = logging.getLogger(component)
Is it better to do this: try { ... } catch (Exception ex) {
I would try something like this but it isn't allowed. function GetDynamicModulesProperties() { var
I'll try to make this as straight forward as possible. Currently our team has
I'll try to explain this better on another question. This is the query that
I've written this to try and log onto a forum (phpBB3). import urllib2, re

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.