Skip to content

How to find all matches of a link in a document using Nokogiri #3420

Discussion options

You must be logged in to vote

@willowlight Your results are going to depend on your input HTML. If possible, it would be great to be able to reproduce what you're seeing in a complete running example in a single file like this:

#!/usr/bin/env ruby

require "bundler/inline"

gemfile do
  source "https://rubygems.org"
  gem "nokogiri"
end

html = <<~HTML
<html><body>
  <a href="http://example.com">nope</a>
  <a href="https://mylink.com">yes</a>
</body></html>
HTML

doc = Nokogiri::HTML(html)
doc.xpath('//a[@href="https://mylink.com"]').each do |node|
  puts node.text
end
# >> yes

Case-insensitive XPath searches are not supported in XPath 1.0, which is the version implemented by libxml2. If you're dealing with ASCII stri…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@flavorjones
Comment options

Answer selected by flavorjones
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants