I am parsing a web page using HTML::TreeBuilder. I succesfully traversed the page using look_down() to find all links with a 'class' attribute equal to "mailtext". Above each of these links in the tree is a link without any attributes. I'd like to retrieve the 'href' from that link by using look_up(). In other words, for each element I find that matches my criteria, I'd like to traverse up one link in the tree and retreive the url. However, when I print the new element using all_attr(), it appears to be the same element I originally found in the tree ("mailtext"), not the new element one up in the tree.
sub get_birthday { my $content = shift; my $tree = HTML::TreeBuilder->new; $tree->parse($content); my @elements = $tree->look_down('_tag' => 'a'); for my $element (@elements) { my $class_tag = $element->attr_get_i('class'); if ($class_tag eq "mailtext") { my $subject = $element->as_trimmed_text(); my $subject_url = $element->attr_get_i('href'); print "<a href='$subject_url'>$subject</a>\n"; my $touchstone = $element->look_up('_tag' => 'a')->attr_get_i('h +ref'); print $element->look_up('_tag' => 'a')->all_attr() . "\n"; } } } # Sample Code <tr valign="top"> <td width=15 bgcolor="E8F1FA"><input type="checkbox" name="checker12 +0041225" value="120041225"></td> <td width=50 bgcolor="E8F1FA"> <span class="text">Apr 27, 2006 11:29 PM</span> </td> <td width=150 bgcolor="E8F1FA"> <table width="150" border="0" cellspacing="0" cellpadding="0" clas +s="imagetable"> <tr><td> <a href="http://profile.myspace.com/index.cfm?fuseaction=user. +viewprofile&friendID=3847879"> <img src="http://myspace-646.vo.llnwd.net/00242/64/63/24245364 +6_s.jpg" align="absmiddle"> </a> <span class="text"> <a href="http://profile.myspace.com/index.cfm?fuseaction=use +r.viewprofile&friendID=3847879">JASON FEDDY</a> </span> <DIV style="width:80px;height:20px;" ID="UserDataNode2" CLASS= +"DataPoint=OnlineNow;UserID=3847879;"></div> </td></tr> </table> </td> <td width=30 bgcolor="E8F1FA">Replied&nbsp;</td> <td width=220 bgcolor="E8F1FA"><a class="mailtext"href="http://mail. +myspace.com/index.cfm?fuseaction=mail.readmessage&messageID=120041225 +&type=inbox&status=new&Mytoken=8727234F-137A-9B6B-2CAEC273502E282F639 +3477">Hello mate</a> </td> </tr>
The result of $touchstone should be:
http://profile.myspace.com/index.cfm?fuseaction=use +r.viewprofile&friendID=3847879
as this is the url directly above the one above $subject_url . Am I misusing the function? Perhaps once you are working with a single Element, you not longer have access to the entire tree? Does $element then become the 'parent'?

In reply to look_up() in HTML::Element Not Traversing As Expected by initself

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.