`
- 浏览:
1234558 次
-
近期本人闲来没事做了一个程序自动从一些BT网站上抓取数据并且自动发帖到我自己的论坛上,试用了几个月效果比较好,现在公布源代码供perl爱好者参考,我的qq是2637663欢迎广大perl爱好者一起沟通交流。
分几个程序组成
readcokie.pl 获取要上传主机的cookie一次获取永久在主机保存
fatie.pl 抓取源主机数据并自动发帖到目标主机
history.log 保存抓取过的数据
user.txt 发帖时使用的用户列表
..........
具体程序如下
readcokie.pl
======================
# -*- coding: gb2312 -*-
#$ua->post( $url, /%form )
#$ua->post( $url, /@form )
#$ua->post( $url, /%form, $field_name => $value, ... )
#This method will dispatch a POST request on the given $url, with %form or @form providing the key/value pairs for the fill-in form content. Additional headers and content options are the same as for the get() method.
#This method will use the POST() function from HTTP::Request::Common to build the request. See the HTTP::Request::Common manpage for a details on how to pass form content and other advanced features.
#$ua->get( $url )
#$ua->get( $url , $field_name => $value, ... )
#This method will dispatch a GET request on the given $url. Further arguments can be given to initialize the headers of the request. These are given as separate name/value pairs. The return value is a response object. See the HTTP::Response manpage for a description of the interface it provides.
#$ua->agent('Mozilla/5.0');
use HTTP::Cookies;
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
$ua = LWP::UserAgent->new;
open(FILE,'D:/bin/posttools/自动发帖/bt区顶贴/cc8.cnsuk.net新沙加神话/data/user.txt');
@file=<FILE>;
for ($i=0;$i<=$#file;$i++){
chomp($file[$i]);
($user,$pass)=split(/,/,$file[$i]);
$ua->cookie_jar(HTTP::Cookies->new(file => "D://bin//posttools//自动发帖//bt区顶贴//cc8.cnsuk.net新沙加神话//data//$user//cookie.txt",
autosave => 1));
$req =POST 'http://www1.5hxy.com/bbs/login.asp',
[UserName => $user,
Userpass => $pass,
IsSave => '1',
Eremite => '1',
];
$res=$ua->request($req);
$ua->cookie_jar->save;
# $ua = LWP::UserAgent->close;
}
fatie.pl
=============================
use HTTP::Cookies;
use LWP;
$ua = LWP::UserAgent->new;
$ua->agent("Mozilla/8.0"
;
#初始化参数
$n_file='D:/bin/posttools/自动发帖/bt区顶贴/cc8.cnsuk.net新沙加神话//';
###临时种子文件地址
#定义地址@url @postid @bankuainame
$hhttp='http://cc8.cnsuk.net';
$url[0]='http://cc9.cnsuk.net/forum-2944-2.html';
$postid[0]='13';
$bankuainame[0]="$hhttp-游戏";
$url[1]='http://cc9.cnsuk.net/forum-2944-1.html';
$postid[1]='13';
$bankuainame[1]="$hhttp-游戏";
$url[2]='http://cc9.cnsuk.net/forum-2905-2.html';
$postid[2]='13';
$bankuainame[2]="$hhttp-游戏";
$url[3]='http://cc9.cnsuk.net/forum-2905-1.html';
$postid[3]='13';
$bankuainame[3]="$hhttp-游戏";
while (1) {
for ($u=0;$u<=$#url;$u++){
$htms='';
$res = $ua->get($url[$u]);
$htms=$res->content;
print "获取 $bankuainame[$u] 版块数据 /n";
#获取后挑出有用的数据
if ($res->is_success) {
@html=();
@html=split(//n/,$htms);
@link=();
@tid=();
@tti=();
for(@html){
#<a href="thread-50813-1-1.html" style="font-weight: bold;color: green">[04.19][BT游戏最新补丁发布专用贴][暂放]</a><span class="lighttxt">
#if (m{^<a href="(viewthread/.php/?tid=)(/d+)&.*>(.*)</a>}){
if (m{^<a href="(thread/-)(/d*)(/-/d*/-/d*/.html)".*?>(.*?)</a>}){
push(@link,$1.$2.$3);
push(@tid,$2);
push(@tti,$4);
}
}
}
print "挑出有用的数据 $#link 个 /n";
#历史记录
open (FILE,$n_file.'data/history.log');
@history=();
@history=<FILE>;
close FILE;
#用户记录
open(FILE,$n_file.'data/user.txt');
@usertxt=();
@usertxt=<FILE>;
close FILE;
#提交记录
print "读取用户信息 /n";
#检查帖子是否在历史,不在就发帖
$chazhao=0;#是否找到0没找到
for ($x=0;$x<=$#tid;$x++){
for ($a=0;$a<=$#history;$a++){
if ($history[$a]==$tid[$x]){
$a=$#history+1;
$chazhao=1;
}
}
print "帖子$tti[$x] $tid[$x] 找到标志为 $chazhao/n";
if ($chazhao==0){ #如果历史没有就发帖
#获取源帖子内容
$url="$hhttp/$link[$x]";
$res = $ua->get($url);
$htmls=$res->content;
#open (FILE,'>D:/bin/posttools/自动发帖/bt区顶贴/bbs.btpig.com猪猪乐园/bin/temp.log'); #debug
#print FILE $htmls; #debug
#close FILE; #debug
@html=();
@html=split(//n/,$htmls);
@torlink=();
@tortid=();
@torxylink=();
@tortti=();
$zd=0;
#获取所有种子地址
for(@html){
#<a href="viewthread.php?tid=645515&extra=page%3D1" style="font-weight: bold;color: blue">[02.02][原创][美国][二战][二战电影五部][DVDRip][6.1G] 英文字幕</a>
#<a href="attachment.php?aid=26721" target="_blank" class="bold">金庸群侠传全集★cc8cnsuk.net新沙加神话★VItas★.torrent</a> (2007-4-28 21:12, 17.35 K)<br>
if (m{<a href="(attachment/.php/?aid=)(/d+)".+?>(.+/.torrent)</a>}){
push(@torlink,$1.$2);
push(@tortid,$2);
push(@tortti,$3);
$zd=1;
}
}
#找到种子文件才发帖,否则不发帖
if ($zd==1){
$userc=int(rand($#usertxt));#选择哪个用户
chomp($usertxt[$userc]);
($user,$pass)=split(/,/,$usertxt[$userc]);
print "决定用户$userc发帖子/n";
#获取种子
@torxylink=();
for ($f=0;$f<=$#tortid;$f++){
$err=0;
$url="$hhttp/$torlink[$f]";
$res = $ua->get($url,referer=>$hhttp,);
print "种子获取成功,开始上传种子/n";
if ($res->is_success) {
$torrent=$res->content;
$filename="$n_file"."torrent//temp/.torrent";
open (FILE2,">$filename"
;
binmode(FILE2);
print FILE2 $torrent;
close FILE2;
#上传种子
w1:{$ua->cookie_jar(HTTP::Cookies->new(file => "$n_file"."data//$user//cookie/.txt",
autosave => 0));
$ua->timeout(240);
$response = $ua->post('http://www3.5hxy.com/bbs/UploadAttachment.asp',
Content_Type => 'form-data',
Content => [ file => ["$filename"],
],
referer=>'http://www3.5hxy.com/bbs/',);
#获取上传目标种子地址
#<a target=_blank href=UpFile/UpAttachment/2007-2/20072731345.torrent>http://www.5hxy.com/UpFile/UpAtt ... 20072731345.torrent</a>
if ($response->content=~m{(<a target=_blank href=.*?/.torrent.*?>
}m){
push(@torxylink,$1.$tortti[$f].'</a>');
}else{
if ($err<=5){#如果没有错误5次继续尝试上传
$err++;
goto w1;
}
}
last w1;
}
}
print "种子上传完毕/n";
#sleep 3;
}
#拆分源帖子
$zzdaot=0;
$zzdaow=0;
for ($s=0;$s<=$#html;$s++){
#找帖子头部
if ($zzdaot==0){
if ($html[$s]=~m/<table width="95%" border="0" cellspacing="0" cellpadding="0"/){
$tou=$s+39; #+5是头部偏移量
$zzdaot=1;
}
}elsif ($zzdaow==0){
if ($html[$s]=~m/<table width="95%" border="0" cellspacing="0" cellpadding="0"/){
$wei=$s-30; #尾部偏移量
$zzdaow=1;
}
}
}
#如果只找到头没有找到尾那么尾偏移30;
if ($zzdaot==1 and $zzdaow=0){
$wei=$tou+30;
}
#获取所有图片地址
print "获取所有图片地址/n";
@imgh=();
for($b=$tou;$b<=$wei;$b++){
@imgh=split(/ /,$html[$b]);
for($j=0;$j<=$#imgh;$j++){
#<img src="http://img.album.pchome.net/02/71/78/71/efe60b699dcfb8277e0eb309ce4ee1ce.jpg" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window/nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://img.album.pchome.net/02/71/78/71/efe60b699dcfb8277e0eb309ce4ee1ce.jpg');}" onmousewheel="return imgzoom(this);">
if ($imgh[$j]=~m{src="(.+/.jpg)"}){
$imgf=$1;
if ($imgf=~m/http/){
push(@img,"/[img/]$imgf/[/img/]"
;
}else{
push(@img,"/[img/]$hhttp/$imgf/[/img/]"
;
}
}
}
}
print "合并数据准备发帖/n";
$constor='';
#合并种子地址
for ($b=0;$b<=$#torxylink;$b++){
$constor.="<br><br> 本站种子地址
torxylink[$b] <br><br>";
}
@torxylink=();
#合并图片地址
for ($b=0;$b<=$#img;$b++){
$constor.="<br><br> $img[$b] <br><br>";
}
@img=();
#合并要发送的数据
$cons=$constor;
for ($g=$tou;$g<=$wei;$g++){
$cons.=$html[$g];
}
$cons=~s/<i.*?//>//mg;
$cons=~s/<d.*?>//mg;
$cons=~s/<//d.*?>//mg;
$cons.="<br><br>此数据来自$hhttp/$link[$x] <br><br>";
#####开始发帖子哎,写了这么多终于可以发帖子了,真不容易
$ua->cookie_jar(HTTP::Cookies->new(file => "$n_file"."data//$user//cookie/.txt",
autosave => 0));
$ua->timeout(240);
$url='http://www3.5hxy.com/bbs/AddTopic.asp?ForumID='.$postid[$u];
#开始发帖子
print "开始发帖/n";
$response = $ua->post( $url,
[ForumID => $postid[$u],
Subject => $tti[$x] ,
Body => $cons,
UpFileId=>1,
#content => $cons,
#DisableYBBCode => '0'
],
referer=>'http://www3.5hxy.com/bbs/', );
}
print "帖子 $tti[$x]$tid[$x] 记录历史完毕 /n";
open (FILE,">>$n_file".'data/history.log'); #回过帖子的记录起来
print FILE "$tid[$x]/n";
close FILE;
}
print "==================== $bankuainame[$u] =======================/n";
$chazhao=0;
}
}
print "休眠1200秒 /n";
sleep 1200;
}
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
"神奇的Perl例子100个"这个资源显然旨在提供一系列实践性的示例,帮助学习者掌握Perl语言的核心概念和实用技巧。以下是一些可能涵盖的知识点: 1. **变量和数据类型**:Perl支持多种数据类型,包括标量、数组、哈希...
数组索引从0开始,`$array[0]`获取第一个元素。 3. **文件I/O**:Perl提供了打开、关闭、读取和写入文件的简便方法。`open`函数用于打开文件,`<FILEHANDLE>`用于读取,`>FILEHANDLE`用于写入。`close`函数用于关闭...
学习perl语言的可以作参考
这是一个用perl写的perl的ftp密码猜解器中文版的例子
- 访问地址:[http://www.perldoc.com/perl5.6/pod/perlmodlib.html](http://www.perldoc.com/perl5.6/pod/perlmodlib.html),这是一个非常有用的资源,可以查找和学习Perl模块的相关信息。 2. **模块下载源**: ...
"dumpvar.pl"是一个Perl脚本,而"perl_例子"表明这是一个关于Perl编程语言的学习实例。Perl是一种强大的、灵活的文本处理语言,常用于系统管理、网络编程、网页开发等领域。 在Perl的世界里,"dumpvar.pl"可能是一...
在这个例子中,你可能会看到一个名为`CGI.cgi`的文件,这就是Perl CGI脚本。打开这个文件,你将看到Perl代码,这些代码处理HTTP请求,获取表单数据,进行必要的计算或数据处理,然后生成HTML输出。 Perl CGI脚本的...
perl语言函数stat的一个简单的例子,对于初学者可以看看。示例是输出文件的修改日期,文件路径自己修改,文件自己随便选个都行。
perl ssh 程序,一个不错的例子!! 用了net::ssh::expect
- **语法:** `表达式1 && 表达式2`(都为真时返回真),`表达式1 || 表达式2`(任意一个为真时返回真)。 #### 二、循环语句 1. **while循环:** - perl0-10.pl中演示了如何计算1到10的阶乘,使用了`while`循环...
在这个例子中,我们定义了一个名为 `hello` 的 C 函数,它被包含在一个名为 `TEST` 的 Perl 模块中。`EXTERN.h`、`perl.h` 和 `XSUB.h` 是必需的头文件,它们提供了必要的宏和类型定义,以确保 XS 文件能够正确地...
`perl-IPC-Cmd`是Perl的一个模块,它提供了执行外部命令并捕获其输出的功能。离线安装Perl模块在没有互联网连接或者安全策略限制的环境下尤其重要。下面我们将详细介绍如何在Linux上离线安装`perl-IPC-Cmd`,以及...
在Perl编程中,文件句柄是一个非常重要的概念,它用于指向一个打开的文件或设备。Perl提供了三个标准的文件句柄:`STDIN`、`STDOUT` 和 `STDERR`。 - **`STDIN`**: 这个文件句柄代表标准输入,默认情况下是指向键盘...
"Perl_examples"这个压缩包可能包含了各种Perl编程的实例,旨在帮助学习者深入理解和掌握Perl语言的语法和架构。以下是对Perl语言及其相关知识点的详细介绍: 1. **变量与数据类型**:Perl支持多种数据类型,包括...
1. **Perl简介**:介绍Perl的历史、特点和适用领域,让读者对Perl有一个全面的认识。 2. **Perl安装与环境设置**:指导如何在不同的操作系统上安装Perl解释器,并设置开发环境。 3. **Perl语法基础**:包括变量、...
Perl-Cross 是一个用于在不同平台上交叉编译 Perl 解释器的工具集。这个压缩包“perl-cross.tar.gz”包含了所有必要的源代码和脚本,使得开发者能够在一台系统上构建适用于其他操作系统或架构的 Perl 版本。这个过程...
一个常见的应用场景是在C程序中处理文本文件,可以编写一个Perl脚本专门进行复杂的文本分析,然后在C代码中调用这个脚本。例如,以下代码片段展示了如何在C中调用一个Perl脚本读取文件内容: ```c #include #...
1.3 编写你的第一个Perl程序 9 1.3.1 键入程序 9 1.3.2 运行程序 9 1.3.3 程序正确将会发生什么情况 10 1.3.4 Perl程序的具体运行过程 10 1.3.5 必须知道的一些情况 11 1.4 课时小结 12 1.5 课外作业 12 1.5.1 专家...
随着时间的推移,Perl发展成为一种强大的通用编程语言,具有丰富的代码库(Perl 5),并在不断地演变中续写着辉煌的历史(Perl 6)。 - **版本演进**: - **Perl 1**:初始版本,主要用于系统管理和文本处理。 - ...
因其原始设计者Larry Wall喜欢骆驼,所以Perl的官方吉祥物是一只大骆驼,这也体现在“Perl大骆驼”这个称号上。这本书《Perl大骆驼》是Perl学习者的经典入门教材,它深入浅出地介绍了Perl语言的基础知识和高级特性,...