下载网上上一些电子版报纸的perl代码
2008-10-17 11:09
239 查看
#!c:/perl/bin/perl.exe
################################################################################
#
# 下载各大报纸电子版
# get_e-paper.pl -n paperName
# -l 显示当前支持报纸,即-n后可以支持的paperName
#
################################################################################
use warnings;
use strict;
use Getopt::Std;
use LWP;
use URI;
use HTTP::Status;
use Switch;
#use LWP::Debug qw(+);
#use Data::Dumper;
use constant url_dfwb=>'http://dfwb.njnews.cn/';
sub usage(){
my $u=<<'END';
get_e-paper.pl -n paperName
-l 显示当前支持报纸,即-n后可以支持的paperName
zdl0812@163.com
END
print $u;
print "/n/n输入回车退出程序!/n";
<>;
exit -1;
}
sub showPaperList(){
my $list=<<"END";
-n dfwb --东方卫报
END
print $list;
print "/n/n输入回车退出程序!/n";
<>;
exit 0;
}
#需要传入url绝对路径
sub downUrlFile{
my ($list) = shift;
my $downloadFile = substr($list,rindex($list,'/')+1);
use LWP::UserAgent;
my $us_download = LWP::UserAgent->new;
#$us_download->timeout(10);
#$us_download->agent('ReportsBot/1.01');
#
#defined ( my $down_child_id = fork() ) or die "Fail to fork $!/n";
#my $downloadFile_tmp = $downloadFile.'-'.$down_child_id;
#if ($down_child_id) {#父进程
# print "这里是父进程! $$/n";
# print "下载临时文件为: $downloadFile_tmp/n";
# sleep 5;
# while ( -e $downloadFile_tmp ) {
# my $size = (-s $downloadFile_tmp);
# print "已经取得文件大小: $size bytes/n";
# sleep 1;
# }
#} else { #子进程
# print "这里是子进程! $$/n";
# if ($us_download->mirror($list, $downloadFile)) {
# print "下载PDF报纸成功: $downloadFile/n";
# }
#}
if ($us_download->mirror($list, $downloadFile)) {
print "下载PDF报纸成功: $downloadFile/n";
}
return $downloadFile;
}
sub downloadPDF{
my $url = shift;
my $browser = LWP::UserAgent->new;
my $response = $browser->get($url);
die "Can't get $url -- ", $response->status_line
unless $response->is_success;
#取得首页链接内容,分析当日报纸跳转页面
my $html = $response->content;
#print $html,"/n";
if( $html =~ m/<META HTTP-EQUIV="REFRESH" CONTENT="0; URL=(.+?)">/ ){
$url = URI->new_abs( $1, $response->base );
print "$url/n";
}else{
print "没有匹配到需要跳转的当日报纸首页!取得内容为: /n$html/n";
exit -1;
}
#从实际当日主页中获取PDF下载列表
$response = $browser->get($url);
die "Can't get $url -- ", $response->status_line
unless $response->is_success;
$html = $response->content;
#print $html,"/n";
my $paper_page=0;
while( $html =~ m/<a href=(.+?)>/g ) {
my $u=$1;
if ($u =~ m//_pdf/.pdf/){
#print $u ,"/n";
my $pdf_u = URI->new_abs( $u, $response->base );
print $pdf_u,"/n";
my $pdf_file = &downUrlFile($pdf_u);
$paper_page++;
#exit if $paper_page == 3;
}
}
exit;
}
sub downloadPaper{
my ($parerName) = shift;
switch($parerName){
case "dfwb" {&downloadPDF(url_dfwb)}
#case "" {}
else {print "暂时不支持这个报纸的下载!/n"}
}
}
sub main{
my %CmdOpertion;
getopts('n:l', /%CmdOpertion);
my $OptNum = keys %CmdOpertion;
if ($OptNum != 1){
print "参数个数不足: /$OptNum=$OptNum/n";
&usage;
}
&showPaperList if (defined $CmdOpertion{l});
&downloadPaper($CmdOpertion{n}) if (defined $CmdOpertion{n});
}
&main;
################################################################################
#
# 下载各大报纸电子版
# get_e-paper.pl -n paperName
# -l 显示当前支持报纸,即-n后可以支持的paperName
#
################################################################################
use warnings;
use strict;
use Getopt::Std;
use LWP;
use URI;
use HTTP::Status;
use Switch;
#use LWP::Debug qw(+);
#use Data::Dumper;
use constant url_dfwb=>'http://dfwb.njnews.cn/';
sub usage(){
my $u=<<'END';
get_e-paper.pl -n paperName
-l 显示当前支持报纸,即-n后可以支持的paperName
zdl0812@163.com
END
print $u;
print "/n/n输入回车退出程序!/n";
<>;
exit -1;
}
sub showPaperList(){
my $list=<<"END";
-n dfwb --东方卫报
END
print $list;
print "/n/n输入回车退出程序!/n";
<>;
exit 0;
}
#需要传入url绝对路径
sub downUrlFile{
my ($list) = shift;
my $downloadFile = substr($list,rindex($list,'/')+1);
use LWP::UserAgent;
my $us_download = LWP::UserAgent->new;
#$us_download->timeout(10);
#$us_download->agent('ReportsBot/1.01');
#
#defined ( my $down_child_id = fork() ) or die "Fail to fork $!/n";
#my $downloadFile_tmp = $downloadFile.'-'.$down_child_id;
#if ($down_child_id) {#父进程
# print "这里是父进程! $$/n";
# print "下载临时文件为: $downloadFile_tmp/n";
# sleep 5;
# while ( -e $downloadFile_tmp ) {
# my $size = (-s $downloadFile_tmp);
# print "已经取得文件大小: $size bytes/n";
# sleep 1;
# }
#} else { #子进程
# print "这里是子进程! $$/n";
# if ($us_download->mirror($list, $downloadFile)) {
# print "下载PDF报纸成功: $downloadFile/n";
# }
#}
if ($us_download->mirror($list, $downloadFile)) {
print "下载PDF报纸成功: $downloadFile/n";
}
return $downloadFile;
}
sub downloadPDF{
my $url = shift;
my $browser = LWP::UserAgent->new;
my $response = $browser->get($url);
die "Can't get $url -- ", $response->status_line
unless $response->is_success;
#取得首页链接内容,分析当日报纸跳转页面
my $html = $response->content;
#print $html,"/n";
if( $html =~ m/<META HTTP-EQUIV="REFRESH" CONTENT="0; URL=(.+?)">/ ){
$url = URI->new_abs( $1, $response->base );
print "$url/n";
}else{
print "没有匹配到需要跳转的当日报纸首页!取得内容为: /n$html/n";
exit -1;
}
#从实际当日主页中获取PDF下载列表
$response = $browser->get($url);
die "Can't get $url -- ", $response->status_line
unless $response->is_success;
$html = $response->content;
#print $html,"/n";
my $paper_page=0;
while( $html =~ m/<a href=(.+?)>/g ) {
my $u=$1;
if ($u =~ m//_pdf/.pdf/){
#print $u ,"/n";
my $pdf_u = URI->new_abs( $u, $response->base );
print $pdf_u,"/n";
my $pdf_file = &downUrlFile($pdf_u);
$paper_page++;
#exit if $paper_page == 3;
}
}
exit;
}
sub downloadPaper{
my ($parerName) = shift;
switch($parerName){
case "dfwb" {&downloadPDF(url_dfwb)}
#case "" {}
else {print "暂时不支持这个报纸的下载!/n"}
}
}
sub main{
my %CmdOpertion;
getopts('n:l', /%CmdOpertion);
my $OptNum = keys %CmdOpertion;
if ($OptNum != 1){
print "参数个数不足: /$OptNum=$OptNum/n";
&usage;
}
&showPaperList if (defined $CmdOpertion{l});
&downloadPaper($CmdOpertion{n}) if (defined $CmdOpertion{n});
}
&main;
相关文章推荐
- 网上找的一些效果代码
- 网上收集到的一些小脚本代码
- js 在网上找了一些加密的方法 (所有代码来的web)
- java代码实现从网上直接下载图片或者网页
- Java 从网上下载文件的几种方式实例代码详解
- 网上下载的java排序算法代码
- 网上只写了一个文件断点下载的例子! 我仿照别人得例子 自己增加了自己代码,异步下载文件 支持断点下载 多任务下载! 如有问题 请联系我
- javascript的万能查询器根据网上一些朋友的代码个性后得到.
- 哪些年遇到过的Andriod问题(13)Mars的下载文件代码不知道是什么问题,在网上去新COPY了一个就可以下载了。
- 遇到的问题-----网上下载的项目修改代码无效,不能相应的生成相应的页面内容
- 演示程序中用到的一些通用代码提供下载,否则无法编译。
- MAC 10.8系统Mountain Lion,一些从网上下载的.dmg文件,打不开,显示“打不开…,因为它来自身份不明的开发者”如何解决?
- 怎么导入网上下载的Cocos2d-x代码至VS
- 遇到的问题-----网上下载的项目修改代码无效,不能相应的生成相应的页面内容
- AndroidStudio导入网上下载的代码慢及AndroidStudio不提示代码的解决方法
- 网上看的一些代码备份
- 栈区,堆区,全局区,文字常量区,程序代码区 的一些解释(转自网上) 2011-12-9 15:03
- 网上比较流行的一种tab切换效果代码,主要是用于导航的一些栏目。
- 网上收集来的一些代码