semio has asked for the wisdom of the Perl Monks concerning the following question:
I have a simple script that performs a google query, strips excess html and presents the web links, that is everything contained within <a> tags. My previous approach had been to strip off everything surrounding the <a> tags, but I had the thought that simply matching the links would be a cleaner approach. I've been encountering some issues trying to formulate the regex correctly. My regex seems to be matching the entire scalar rather than extracting the result and sending it to my array. Ideally I would like to match everything within <a> tags (including the tags themselves) minus the google ad links and such. This is what I've come up with so far. Any suggestions are greatly appreciated. cheers.
#!/usr/bin/perl -w use strict; use LWP::UserAgent; my $search = 'perlmonks'; my $ua = LWP::UserAgent->new; $ua->agent( "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/200 +70309 Fire fox/2.0.0.3" ); my $req = HTTP::Request->new( GET => "http://www.google.com/search?hl=en&num=100&q=$search&btnG=Googl +e+Search" ); $req->content_type('application/x-www-form-urlencoded'); my $res = $ua->request($req); my $data; if ( $res->is_success ) { $data = $res->content; } else { print $res->status_line; } my @links = $data =~ m/\<a href(.*)\<\/a\>/gs; foreach my $link (@links) { # if ( $link =~ /google/i ) { # next; # } # else { print $link . "this matched\n"; # } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: matching links in a web request
by moritz (Cardinal) on Jun 22, 2007 at 16:29 UTC | |
|
Re: matching links in a web request
by jhourcle (Prior) on Jun 22, 2007 at 16:43 UTC | |
by blazar (Canon) on Jun 26, 2007 at 15:26 UTC |