软件下载 | 资讯教程 | 最近更新 | 下载排行 | 一键转帖 | 发布投稿
您的位置:最火下载站 > 网络编程 > 编程综合 > Perl 实现在网页自动下载资源

Perl 实现在网页自动下载资源

打开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 . "\n" ;
&GetPictture($mypic);
print "\n" ;
}
close PICTURE_IN ;

sub GetPictture
{
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!\n";

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/<a\s+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 "\n$title_utf8_gb\n" ;
print "\n$picture\n" ;
$url = $picture ;
my @header = head($url);
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,"%","\t\t\t\r";
}

1;

以上脚本保存为.pl即可。
    相关阅读
    栏目导航
    推荐软件