- 论坛徽章:
- 0
|
require 5;
use strict;
use warnings;
use utf8;
use URI::Escape;
use Encode;
foreach my $word (@ARGV) {
next unless length $word; # sanity-checking
my $url = "http:". "//". "www.". "sogou". ".com". "/sohu?query="
. uri_escape($word) . '';
my ($content, $status, $is_success) = do_GET($url);
$content= decode( 'utf8', $content);
if (!$is_success) {
print "Sorry, failed: $status\n";
} elsif ($content =~ m{找到约<resnum id="scd_num">(.+)</resnum>条}) { # like "1,952"
print "$word: $1 matches\n";
} else {
print "$word: Page not processable, at $url\n";
}
sleep 2; # Be nice to Altavista's servers!!!
}
# And then my favorite do_GET routine:
use LWP; # loads lots of necessary classes.
my $browser;
sub do_GET {
$browser = LWP::UserAgent->new unless $browser;
my $resp = $browser->get(@_);
return ($resp->content, $resp->status_line, $resp->is_success, $resp)
if wantarray;
return unless $resp->is_success;
return $resp->content;
}
C:\Users\Administrator\Desktop\perl\perl\network>perl alta_count.pl clinton trump
clinton: 150,584 matches
trump: 488,868 matches |
评分
-
查看全部评分
|