http://qs321.pair.com?node_id=589115


in reply to Process a HTML file to get information from it.

HTML::TreeBuilder is a pretty good tool for this sort of work, especially if the format of the HTML is consistent for the data you need to extract. Consider:

use strict; use warnings; use HTML::TreeBuilder; my $root = HTML::TreeBuilder->new (); $root->parse_file (*DATA); for ($root->look_down ('_tag', 'a')) { my $href = $_->attr ('href'); next if ! $href; my $sib = $_->right ()->right (); my $number = $sib->as_text (); print "$href: $number\n"; } __DATA__
<a name="a"></a> <h2>A</h2> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td> <table id="a" border="1" bordercolor="#333366" cellpadding="5 +" cellspacing="0" width="100%"> <tr> <td width="33%" class="clsTableBody" valign="top" id="f +irstCol"> <a href="pdf\c76b834e-36e1-497b-b13e-eba2348dc044. +pdf" target="_blank">Abbott, Evelyn</a><br/><span>110136892</span><br +/> <a href="pdf\8a956f66-1c60-48fc-905c-b49d617aa6c5. +pdf" target="_blank">Agnew, Thomas</a><br/><span>110377660</span><br/ +></td> <td width="34%" class="clsTableBodyClear" valign="top" +id="secondCol"><a href="pdf\37d3e78b-1adb-458b-9e89-0df780909f08.pdf" + target="_blank">Allison, David</a><br/><span>108116112</span><br/><a + href="pdf\6c0a5bb4-143d-4305-957b-796c8193d07a.pdf" target="_blank"> +Allison, Gary Owen</a><br/><span>116815754</span><br/></td> <td width="33%" class="clsTableBody" valign="top" id="t +hirdCol"><a href="pdf\ae8d51e0-005b-44be-84cb-3c9b57335755.pdf" targe +t="_blank">Arsenault, Michael</a><br/><span>108318866</span><br/><a h +ref="pdf\e646f948-f78d-4463-a01d-0261aebf70dc.pdf" target="_blank">Ar +senault, Normand A.</a><br/><span>113069066</span><br/></td> </tr> </table> </td> </tr> </table>

Prints:

pdf\c76b834e-36e1-497b-b13e-eba2348dc044.pdf: 110136892 pdf\8a956f66-1c60-48fc-905c-b49d617aa6c5.pdf: 110377660 pdf\37d3e78b-1adb-458b-9e89-0df780909f08.pdf: 108116112 pdf\6c0a5bb4-143d-4305-957b-796c8193d07a.pdf: 116815754 pdf\ae8d51e0-005b-44be-84cb-3c9b57335755.pdf: 108318866 pdf\e646f948-f78d-4463-a01d-0261aebf70dc.pdf: 113069066

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Process a HTML file to get information from it.
by Griffler (Novice) on Dec 11, 2006 at 19:06 UTC
    I tried your code and I got the following error.... Can't call method "right" without a package or object reference at C:\Change\2539\testit2.pl line 21. I modified the code to look like this:
    use strict; use warnings; use HTML::TreeBuilder; my $root = HTML::TreeBuilder->new (); $root->parse_file ("c:\\change\\2539\\index.html"); for ($root->look_down ('_tag', 'a')) { my $href = $_->attr ('href'); next if ! $href; my $sib = $_->right ()->right (); my $number = $sib->as_text (); print "$href: $number\n"; }