Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

anchor text match

by kumar801012 (Initiate)
on Dec 27, 2009 at 17:56 UTC ( #814533=perlquestion: print w/replies, xml ) Need Help??

kumar801012 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Perl Monks, I am working on parsing html to obtain all the hrefs that match a particular url(lets call it "target url") and then get the anchor text. I have used TreeBuilder, TokeParser etc but if html contains a link like below:

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>

If my target url is "http://www.yahoo.com", since there is no anchor text, I get the text in alt text of the img tag("Open this result in new window") as the anchor text.

I was wondering if any one can help me out with a regexp to parse all the anchor tags and if the href matches my target url, then return anchor text if exists or "image" if there is a img tag.

Thanks in advance.

Replies are listed 'Best First'.
Re: anchor text match
by JadeNB (Chaplain) on Dec 27, 2009 at 21:06 UTC
    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.
      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).

Re: anchor text match
by Anonymous Monk on Dec 27, 2009 at 19:14 UTC
    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

      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: perlquestion [id://814533]
Approved by AnomalousMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2021-05-09 20:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (102 votes). Check out past polls.

    Notices?