Forum: Ruby Newbie Metchanize Question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Becca G. (Guest)
on 2008-10-31 01:58
Hello.

I have a page that I get in mechanize.

I can view all of the links on the page, but I actually only want to get
some of the links on the page.

How can I grab only what I need?

The source code looks like this:

<div class="details">
  <a href="website/unique_link/id">link</a>
</div>


I want to get the url for each of the details classes or get only the
url's that contain "unique_link" in the them.

Right now I'm using this code to get all of the results.

search_results.links.each.do |link|
  puts link.text
end

THANKS!
Becca G. (Guest)
on 2008-10-31 01:59
Sorry for the misspelling of the subject in the previous post!
Becca G. (Guest)
on 2008-10-31 02:16
Figured it out.

page.search("div.details").search("a[@href*='unique_id']")
Becca G. (Guest)
on 2008-10-31 02:38
Becca G. wrote:
> Figured it out.
>
> page.search("div.details").search("a[@href*='unique_id']")


After re-reading the documentation, I see that I should be able to
simply do something like this --> page.links.href('/unique_id') but I
get nothing.  No results, nada.  Am I doing this wrong?

The previous code works, but I think that I should do this more
efficiently.
Aaron P. (Guest)
on 2008-10-31 03:31
(Received via mailing list)
On Fri, Oct 31, 2008 at 09:38:00AM +0900, Becca G. wrote:
> Becca G. wrote:
> > Figured it out.
> >
> > page.search("div.details").search("a[@href*='unique_id']")
>
>
> After re-reading the documentation, I see that I should be able to
> simply do something like this --> page.links.href('/unique_id') but I
> get nothing.  No results, nada.  Am I doing this wrong?

You can do:

  page.links_with(:href => /unique_id/)

Which will search links that have href's which match /unique_id/

Hope that helps.
This topic is locked and can not be replied to.