Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

parse link and get the text for it from a table

Posted on 2003-02-19
6
Medium Priority
?
222 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
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
Technology Partners: 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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

580 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