Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re^3: anchor text match

by ww (Archbishop)
on Dec 30, 2009 at 19:21 UTC ( #814980=note: print w/replies, xml ) Need Help??

in reply to Re^2: anchor text match
in thread anchor text match

"I have tried...."

is as uninformative as a classic error-in-posting, namely:

"...doesn't work."

Try showing us what you've tried (minimal cases which fail, please).

Replies are listed 'Best First'.
Re^4: anchor text match
by kumar801012 (Initiate) on Dec 30, 2009 at 20:57 UTC
    #!/usr/bin/perl -- use strict; use warnings; use HTML::TreeBuilder; my $html = <<'__HTML__'; <a href="" target=_blank><img src="" alt="Open this result in new window">ANCHOR TEXT</a> <a href="" target=_blank><img src="" 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' ) ) { 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 :; ANCHOR TEXT; Two clues for the price of one

    But the desired output is :; ANCHOR TEXT; IMAGE (indicating no anchor text and also presence of img tag within anchor tag )

    Any ideas?

      You have just copied the code from Re^2: anchor text match literatim (except for some mild massaging of the input). What have you tried?
        I also tried this:
        use WWW::Mechanize(); my $mech = WWW::Mechanize->new(); my $html = $mech->get(''); my @links= $mech->find_all_links( text_regex => qr/a/i ); foreach(@links){ if($_->url() eq ''){ print "\n"; print "url \n"; print $_->url(); print "\n"; print " text\n"; print $_->text(); print "\n"; } } _END_

        The out put is :


        text: Victoria's Secret

        In case the page had an anchor tag like below:

        a href="" target=_blank><img src= height=11 width=11 border=0 alt="Open this result in new window"> </anchor>

        The above perl script would give :


        text: Open this result in new window

        But the desired result is:


        text: IMAGE

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2021-05-08 16:55 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (96 votes). Check out past polls.