欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php 论坛采集程序 模拟登陆,抓取页面 实现代码

程序员文章站 2022-06-15 23:24:47
复制代码 代码如下:
复制代码 代码如下:

<?php
// 吴燕军
// 2009-06-27
// 采集程序php
set_time_limit(0);
//cookie保存目录
$cookie_jar = '/tmp/cookie.tmp';
/*函数------------------------------------------------------------------------------------------------------------*/
//模拟请求数据
function request($url,$postfields,$cookie_jar,$referer){
$ch = curl_init();
$options = array(curlopt_url => $url,
curlopt_header => 0,
curlopt_nobody => 0,
curlopt_port => 80,
curlopt_post => 1,
curlopt_postfields => $postfields,
curlopt_returntransfer => 1,
curlopt_followlocation => 1,
curlopt_cookiejar => $cookie_jar,
curlopt_cookiefile => $cookie_jar,
curlopt_referer => $referer
);
curl_setopt_array($ch, $options);
$code = curl_exec($ch);
curl_close($ch);
return $code;
}
//获取帖子列表
function getthreadslist($code){
preg_match_all('/ <!--[.|\r|\n]*? <a href=\"viewthread.php\?tid=(\d+)/',$code,$threads);
return $threads[1];
}
//判断该帖子是否存在
function isexits($code){
preg_match('/ <p>指定的主题不存在或已被删除或正在被审核,请返回。 <\/p>/',$code,$error);
return isset($error[0])?false:true;
}
//获取帖子标题
function gettitle($code){
preg_match('/ <h1>[^ <\/h1>]*/',$code,$title_tmp);
$title = $title_tmp[0];
return $title;
}
//获取帖子作者:
function getauthor($code){
preg_match('/ <a href=\"space.php\?uid=\d+\" target=\"_blank\" id=\"userinfo\d+\" onmouseover=\"showmenu\(this\.id\)\">.+/',$code,$author_tmp);
$author = strip_tags($author_tmp[0]);
return $author;
}
//获取楼主发表的内容
function getcontents($code){
preg_match('/ <div id=\"postmessage_\d+\" class=\"t_msgfont\">(.|\r|\n)*? <\/div>/',$code,$contents_tmp);
$contents = preg_replace('/images\//','http://bbs.war3.cn/images/',$contents_tmp[0]);
return $contents;
}
//打印帖子标题
function printtitle($title){
echo " <strong> <h2>帖子标题: </h2> </strong>",strip_tags($title)," <br/> <br/>";
}
//输出帖子作者
function printauthor($author){
echo " <strong> <h2>帖子作者: </h2> </strong>",strip_tags($author)," <br/> <br/>";
}
//打印帖子内容
function printcontents($contents){
echo " <strong> <h2>作者发表的内容: </h2>",$contents," </strong> <br/>";
}
//错误
function printerror(){
echo " <i>该帖子不存在! </i>";
}
/*函数列表end---------------------------------------------------------------------------------------------------*/
/*登录论坛 begin*/
$url = 'http://bbs.war3.cn/logging.php?action=login';
$postfields='loginfield=username&username=1nject10n& password=xxxxxx&questionid=0&cookietime=315360000& referer=http://bbs.war3.cn/&loginsubmit=提交';
request($url,$postfields,$cookie_jar,'');
unset($postfields,$url);
/*登录论坛 end*/
/*获取帖子列表(位于第一页的帖子) begin*/
$url = 'http://bbs.war3.cn/forumdisplay.php?fid=57';
$code = request($url,'',$cookie_jar,'');
$threadslist = getthreadslist($code);
/*获取帖子列表 end*/
//帖子序列
$rows = 0;
/*循环抓取所有帖子源代码 begin*/
foreach($threadslist as $list){
$url = "http://bbs.war3.cn/viewthread.php?tid=$list";
if(isexits($code)){
$code = request($url,'',$cookie_jar,'');
$color = $rows%2==0?'#00ccff':'#ffff33';
echo " <div style='background-color:$color'>";
echo " <h1>第",($rows+1),"贴: </h1> <br/>";
$author = getauthor($code);
printauthor($author);
$title = gettitle($code);
printtitle($title);
$contents = getcontents($code);
printcontents($contents);
echo " </div>";
$rows++;
}
else
printerror();
echo "----------------------------------------------------------------------------------------- <br/> <br/>";
}
/*抓取源代码 end*/
?>