in reply to Re^3: anchor text match
in thread anchor text match
#!/usr/bin/perl -- use strict; use warnings; use HTML::TreeBuilder; my $html = <<'__HTML__'; <a href="http://www.yahoo.com" target=_blank><img src="http://us.i1.yimg.com/nw.gif" alt="Open this result in new window">ANCHOR TEXT</a> <a href="http://www.yahoo.com" target=_blank><img src="http://us.i1.yimg.com/nw.gif" alt="Two clues for the price of one"></a> __HTML__ { my $h = HTML::TreeBuilder->new_from_content($html); for my $link ( $h->look_down( _tag => q{a}, href => 'http://www.yaho +o.com' ) ) { print $link->attr('href'),"\n"; my $text = $link->as_trimmed_text; unless ($text) { $text = join ' ', map { $_->attr('alt') } $link->look_down( alt => qr/^.+$/ ); } print "$text\n\n"; } ## end for my $link ( $h->look_down...) } __END__
The output is :
http://www.yahoo.com; ANCHOR TEXT
http://www.yahoo.com; Two clues for the price of one
But the desired output is :http://www.yahoo.com; ANCHOR TEXT
http://www.yahoo.com; IMAGE (indicating no anchor text and also presence of img tag within anchor tag )
Any ideas?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^5: anchor text match
by JadeNB (Chaplain) on Dec 30, 2009 at 21:08 UTC | |
by kumar801012 (Initiate) on Dec 30, 2009 at 21:59 UTC | |
by JadeNB (Chaplain) on Dec 31, 2009 at 00:05 UTC |
In Section
Seekers of Perl Wisdom