ruby - How do I select a tag with a given attribute whose value is unknown? -
i'm guessing trivial question bit of experience nokogiri, haven't been able find answer in documentation or tutorials i've found online.
i have nokogiri document this:
page = nokogiri::html(open("http://www.example.com")) and page contains following tag:
<a title="could anything" href="http://www.example.com/foo"></a> how value of href if value of title unknown?
if want value of href attribute a elements having title attribute can use nokogiri's xpath follows:
require 'nokogiri' doc = nokogiri::html(file.open('sample.html')) a_with_title = doc.xpath('//a[@title]').map { |e| puts e['href'] } if want select url online can use
require 'nokogiri' require 'open-uri' doc = nokogiri::html(open('http://stackoverflow.com/')) a_with_title = doc.xpath('//a[@title]').map { |e| puts e['href'] }
Comments
Post a Comment