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

Re: anchor text match

by JadeNB (Chaplain)
on Dec 27, 2009 at 21:06 UTC ( #814543=note: print w/replies, xml ) Need Help??


in reply to anchor text match

As Anonymonk points out, the answer to “Is there a good way to parse $nested_format with a regex?” is (almost always) no. You say you've tried some members of the HTML:: family of modules; if you show the code you came up with in your attempts, then it'll be a lot easier to see how to adapt it for your needs.

Replies are listed 'Best First'.
Re^2: anchor text match
by kumar801012 (Initiate) on Dec 30, 2009 at 17:35 UTC
    I have tried LinkExtractor, TokenParser, Mechanize, TreeBuilder modules. For below html:

    a href="http://www.yahoo.com" target=_blank><img src=http://us.i1.yimg.com/nw.gif height=11 width=11 border=0 alt="Open this result in new window"> </anchor>

    all of them give "Open this result in new window" as the anchor text.

    Ideally I would like to see blank value or a string like "image" returned so that I know there was no anchor text but the href still matched the target url(http://www.yahoo.com in this case). Is there a way to tweak those modules to get the desired result?

    Thanks,
      "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).

        #!/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?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2020-03-30 20:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    To "Disagree to disagree" means to:









    Results (176 votes). Check out past polls.

    Notices?