Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: anchor text match

by Anonymous Monk
on Dec 27, 2009 at 19:14 UTC ( #814541=note: print w/replies, xml ) Need Help??


in reply to anchor text match

I was wondering if any one can help me out with a regexp to parse all the

Keep on using the HTML:: modules, no need to invent a regex

Replies are listed 'Best First'.
Re^2: anchor text match
by Anonymous Monk on Dec 27, 2009 at 19:28 UTC
    like this
    #!/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"></a> <a href="http://www.yahoo.com" target=_blank><img src="http://us.i1.yimg.com/nw.gif" alt="Two clues"><img src="http://us.i1.yimg.com/nw.gif" alt="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__ http://www.yahoo.com Open this result in new window http://www.yahoo.com Two clues for the price of one
      Thanks for the code. But, for the given HTML:
      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 MATCH</a> <a href="http://www.yahoo.com" target=_blank><img src="http://us.i1.yimg.com/nw.gif" alt="Two clues"><img src="http://us.i1.yimg.com/nw.gif" alt="for the price of one"></a> __HTML__
      Desired result for http://www.yahoo.com as target url is:

      http://www.yahoo.com ANCHOR TEXT MATCH

      http://www.yahoo.com IMAGE (indicating there is no anchor text but there is an <img> tag within anchor tag)

      Thanks,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://814541]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2021-05-06 16:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (77 votes). Check out past polls.

    Notices?