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
The problem is you’re trying to use the
starts_withmethod on an object that doesn’t implement it.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:So, you want to use something like this:
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-withfunction:which outputs:
Here’s how to do it using XPath:
Which outputs:
The advantage to using XPath to find the nodes is it all runs in compiled C, rather than letting Ruby do it.