William Flanagan
2007-Sep-23 14:52 UTC
[Mechanize-users] Selecting Links with their parent class attribute?
Hi all, Trying to figure out a quick way to do something. I have a yahoo groups list, and it has listings that look like this: </td></tr> <tr><td colspan="2" class="ygrp-g"> <span class="ygrp-nowrap"> <em><a href="/group/freecycledc/">freecycledc</a></em> </span> <a href="/group/freecycledc/"><img src="http://f9g.yahoofs.com/groups/g_11337116/.HomePage/__tn_/4d04.jpg?grA.E.GBtlOnTmsC" width="96" height="48" alt=""></a> <ul class="ygrp-ul ygrp-grpsa"><li class="smalltype ygrp-nowrap"> <a href="/group/freecycledc/messages?o=1"><span class="ygrp-grpsan">394</span> New Messages</a> </li> <li class="smalltype ygrp-nowrap"> <a href="/group/freecycledc/spnew?"><span class="ygrp-grpsan">9</span> New Photos</a> </li> </ul> </td></tr> Basically, right now I''m trying to get a group listing. I would think I could select links based on the "ygrp-nowrap" class variable, but it''s not working. Any advice on the how to select the hrefs (and strings) in this type of a configuration? Thanks, Will -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/mechanize-users/attachments/20070923/6766a94a/attachment.html
Aaron Patterson
2007-Sep-23 20:22 UTC
[Mechanize-users] Selecting Links with their parent class attribute?
Hey Will, On Sun, Sep 23, 2007 at 10:52:51AM -0400, William Flanagan wrote:> Hi all, > > Trying to figure out a quick way to do something. I have a yahoo groups > list, and it has listings that look like this: > > </td></tr> > <tr><td colspan="2" class="ygrp-g"> > <span class="ygrp-nowrap"> > <em><a href="/group/freecycledc/">freecycledc</a></em> > </span> > <a href="/group/freecycledc/"><img > src="http://f9g.yahoofs.com/groups/g_11337116/.HomePage/__tn_/4d04.jpg?grA.E.GBtlOnTmsC" > width="96" height="48" alt=""></a> > <ul class="ygrp-ul ygrp-grpsa"><li class="smalltype ygrp-nowrap"> > <a href="/group/freecycledc/messages?o=1"><span > class="ygrp-grpsan">394</span> New Messages</a> > </li> > <li class="smalltype ygrp-nowrap"> > <a href="/group/freecycledc/spnew?"><span class="ygrp-grpsan">9</span> > New Photos</a> > > </li> > </ul> > </td></tr> > > > Basically, right now I''m trying to get a group listing. I would think I > could select links based on the "ygrp-nowrap" class variable, but it''s not > working. Any advice on the how to select the hrefs (and strings) in this > type of a configuration?Mechanize lets you get ahold of the hpricot object and lets you click on links returned from hpricot. So I would suggest using hpricot searching for this task. I think the following example may be close to what you''re looking for: page.search("//li[@class=''smalltype ygrp-nowrap'']/a").each do |link| puts link.inner_text f = mech.click(link) end Hope that helps! -- Aaron Patterson http://tenderlovemaking.com/