2

I'm trying to use the Mechanize link_with(:href => 'anchor here') in order to find pages that have links with a certain string in the href. For example I want it so that I can spit out into a text file all sites that have a link where the anchor contains "index.php?user"

How would I go about this?

3 Answers3

5

Thanks all for your answers, I ended up going with page.link_with(:href => /(.*)?user$/)

1
urls = ['http://www.google.com/','http://www.foo.com/','http://www.bar.com/']

File.open('output.txt', 'w') do |out|
  urls.each do |url|
    out << url if agent.get(url).link_with(:href => /index.php\?user/)
  end
end
pguardiario
  • 53,827
  • 19
  • 119
  • 159
0

I would suggest you look into XPath selectors:

jQuery Xpath selector to select an element which id contains 'sometext'

An example on how to use XPath with mechanize can be found here:

extract single string from HTML using Ruby/Mechanize (and Nokogiri)

Community
  • 1
  • 1
leifg
  • 8,668
  • 13
  • 53
  • 79