Perl 实现在网页自动下载资源
来源:程序员人生 发布时间:2013-10-14 01:39:21 阅读次数:2576次
打开CMD,cd到脚本所在路径,运行脚本,所指定的资源会写在picture_files.txt文件里,我把要添加的资源写在程序里。
#!/usr/bin/perl
use strict;
use warnings;
#use Encode ;
use LWP::UserAgent;
use HTML::TokeParser;
use LWP::Simple ;
my $picture_in = "picture_files.txt" ;
open OUT,">$picture_in";
print OUT "Koeh-232.jpg";
close OUT;
our $counter = 0;
our $picture_file ;
our ($url ,$agent,$request,$response,$document,$ua) ;
my $mypic ;
open "PICTURE_IN", "<", $picture_in || &Abort ("Input file " . $picture_in . " could not be opened.") ;
my @picture_array = <PICTURE_IN>;
foreach $mypic (@picture_array)
{ chomp ($mypic) ;
print "Downloading..." . $mypic . "" ;
&GetPictture($mypic);
print "" ;
}
close PICTURE_IN ;
sub GetPictture
veryhuo.com
{
my $picture_name = shift ;
$url='http://zh.wikipedia.org/wiki/File:' . $picture_name;
$agent=new LWP::UserAgent();
$request=new HTTP::Request('GET'=>$url);
$response=$agent->request($request);
$document=$response->content()||die "get the page failed!";
my $file ='';
$url =~ s/.*/([^/]+)$/($a=$1 , $a =~ s^file:^^i,$file = $a )/eo ;
#<a href="http://upload.wikimedia.org/wikipedia/commons/f/f0/Koeh-232.jpg">
my (@pictures ,$picture) ;
$document =~ s/<as+href="([^"]+)">/(push @pictures ,$1)/geo ;
foreach $picture (@pictures)
{
if($picture =~ m/http/ &&$picture =~ m/$file/)
{
#my $title_utf8_gb = decode("utf8", $picture);
#$title_utf8_gb = encode("gbk", $title_utf8_gb);
#print "$title_utf8_gb" ;
print "$picture" ;
$url = $picture ;
my @header = head($url);
veryhuo.com our $size = $header[1];
@header=();
$picture =~ s/.*/([^/]+)$/($file = $1 )/eo ;
$ua = LWP::UserAgent->new;
$request = HTTP::Request->new('GET',$url);
$response = $ua->request($request);
$response = $ua->request($request,$file);
$response = $ua->request($request,&callback);
last ;
}
}
return ;
}
sub callback {
my ($data,$resp,$proto) = @_;
our $size;
print "#";
my $len = length $data;
our $counter = $counter+$len;
#my $percent = $counter/$size;
#print "Percent = ",$percent*100,"%"," ";
}
1;
以上脚本保存为.pl即可。
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠