?
Solved

parse link and get the text for it from a table

Posted on 2003-02-19
6
Medium Priority
?
218 Views
Last Modified: 2010-03-05
here is the html file i've got:  it has a table with 2 columns and multiple rows.

<table>

<tr>
<td>
<b>Yahoo</b>
</td>

<td>
<a href="http://www.yahoo.com">here</a>
</td>

</tr>

<tr>
<td>
<b>Hotmail</b>
</td>
<td>
<a href="http://www.hotmail.com">here</a>
</td>

</tr>
....

</table>

=========
I want to extract the TEXT along with the link.  I used LinkExtractor package.  But as you can see the Text associated with the links are not particular descriptive.  

Is there another way so that the Text in the first column be extracted and associated with the link?

0
Comment
Question by:crest
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 10

Expert Comment

by:rj2
ID: 7985939
#!/usr/bin/perl

$html=<<ENDOFHTML;
<html>
<body>

<table>

<tr>
<td>
<b>Yahoo</b>
</td>

<td>
<a href="http://www.yahoo.com">here</a>
</td>

</tr>

<tr>
<td>
<b>Hotmail</b>
</td>
<td>
<a href="http://www.hotmail.com">here</a>
</td>

</tr>


</table>
</body>
</html>
ENDOFHTML

while($html=~m!<td>\s*(?:<b>)?(\w*)(?:</b>)?\s*</td>\s*<td>\s*<a href="([^">]*)!ig) {
     print $1,",",$2,"\n";
}
0
 
LVL 2

Expert Comment

by:ChrisDrake
ID: 7988935
I think he means to find the links, not just parse that example.

remember that the <A> tag might not have quotes
<A href=http://hotmail.com>
<A href="http://hotmail.com">
<A href='http://hotmail.com'>

and might have targets

<A target=_blank href=http://hotmail.com>

and there might be other HTML in the text

<A href=http://hotmail.com>an <i>expensive</i> free webmail</a>

... so it's probably about 10 lines of perl to do properly.
0
 
LVL 1

Expert Comment

by:unobserved
ID: 7988955
Use the HTML::TokeParser::Simple module from CPAN.

It's the easiest, more efficient and correct way to do it.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:crest
ID: 7991063
thanks for all your comments.  yes i want a more robust solution.  unobserved suggestion of using HTML::TokeParser:Simple sounds good.  i even had a look at the source code of HTML::LinkExtractor which uses the TokeParser::Simple module.  but the oo just look too much for me...

it would be a great chance for me to learn more about perl's oo capability... there are so many module out there that use this methodology.  so would be useful.  but i need some guidance on how to go about getting text from different 'cell' to be related to a link in yet another cell. cheers.
0
 
LVL 10

Accepted Solution

by:
rj2 earned 600 total points
ID: 7997623
#!/usr/bin/perl
use strict;
use HTML::TokeParser::Simple;

my $p = HTML::TokeParser::Simple->new('parsehtml.html');
my ($tdcount,$linktext,$savetext,$savelink,$testtext,@text);


while ( my $token = $p->get_token ) {
     if ( $token->is_start_tag('td')) {
          $tdcount++;
          if($tdcount % 2) {
               $savetext=1;
          } else {
               $savelink=1;
          }          
     }
     if ( $token->is_end_tag('td')) {
          $savetext=0;
          $savelink=0;
     }
     if($savetext && $token->is_text()) {
          $testtext=$token->as_is();
          chomp($testtext);
          if(length($testtext)) {    
               $linktext=$testtext;              
          }
         
     }
     if ($savelink && $token->is_start_tag('a')) {
          my $attr=$token->return_attr();
          push(@text,$linktext . ',' . $attr->{ href });                    
     }
}
foreach (@text) {
     print $_,"\n";
}    
0
 

Author Comment

by:crest
ID: 8006598
rj2, thanks very much for your answer.

your approach is simple and effective. i can adopt it to many different situations.

cheers
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question