perl LWP::UserAgent抓捕騰訊音樂MV點擊量

抓捕騰訊音樂MV前100頁的點擊量

[root@dou ~]# cat lwp.pl
#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
use Encode;

my $num;
my $total;
my @nums = (0..99);
my @firstnums = (0..9) x 10;
my $firstnum = 0;
my $html_result;
my $file = "tmp1";
my $filetmp = "tmpfile";
my $ua = LWP::UserAgent->new;
$ua->timeout(3);

foreach my $nums (@nums) {
        my $url = "http://v.qq.com/mvlist/$firstnums[$firstnum]/22\_-1\_-1\_-1\_-1\_1\_$nums\_0\_28.html";
        my $response = $ua->get($url);
        if ($response->is_success) {
                $html_result = encode_utf8($response->decoded_content);
                open(my $fh,'>',"$file") or die "can't create file $file:$!\n";
                print $fh $html_result;
                close $fh;
                open(my $in,'<',"$file") or die "can't open file $file: $!\n";
                while(<$in>) {
                        next unless /_total_view/;
                        /.*"_total_view">(\d+)<\/span>.*/;
                        $num = $1;
                        open(my $tmpfile,'>>',"$filetmp")or die "can't create file $filetmp:$!\n";
                        print $tmpfile "$num\n";
                        $total += $num;
                    }
        $firstnum += 1;
        close $in;
        } else {
                print "sorry, request failed at [ $firstnum ] -> [ $nums ]\n";;
        }
}
print "$total\n";

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章