Rubyx Rubyx - 2 months ago 11
Ruby Question

Get HREF values without CLASS or ID using Mechanize

i'm trying to get HREF value on a page but there's no CLASS or ID.

<div class="columns">
<a href="http://www.website.com/page.html?id=123"></a>
</div>


I tried,

a = Mechanize.new
page = a.get("http://www.website.com/index.html")
url = page.links_with(:href => "^http://www.website.com/page.html?id=").href
puts url[0]


Any idea? thx

Answer

Try Following.

a = Mechanize.new
page = a.get("http://www.website.com/index.html")
url = page.link_with(:text => "http://www.website.com/page.html?id=")
puts url

Updated Answer:

a = Mechanize.new
page = a.get("http://www.sainte-maxime.com/-locations-de-vacances-.html")
urls = page.links_with(:href => /.*?id\=/).map{|h| h.href}
puts urls
Comments