Вопрос

I would like to know with nokogiri gem if it's possible to know if a link with an anchor and a text exist in a webpage.

external_url = http://anydomain.com/anyurl
doc = Nokogiri::HTML(external_url.body)
.
.

a link inside of external_url:

<a href="http://url1.com" >url1</a>

I have these data:

href = http://url1.com
link_text = url1

I need to know with nokogiri (I can not use mechanize gem) if it's possible to know if exist inside ofexternal_url a link with href = http://url1.com and with a text url1

Thanks

Это было полезно?

Решение

You can use at_xpath method for this :

require 'open-uri'

external_url = 'http://anydomain.com/anyurl'
doc = Nokogiri::HTML(open(external_url))

href = 'http://url1.com'
link_text = 'url1'

node = doc.at_xpath("//a[@href = '#{href}' and . = '#{link_text}']")
puts "link not found" if node.nil?
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top