techsir 登陆 |注册|TS首页
  首页 快活林 猿氏悟语

使用php的curl采集diszuz论坛

By: 盖茨 发表于 2011-12-29 04:31:03 · 74478次点击   回复:1   
<?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://www.eb163.com/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://www.eb163.com/logging.php?action=login';
$postfields='loginfield=username&username=1nject10n&password=xxxxxx&questionid=0&cookietime=315360000&referer=http://www.eb163.com/&loginsubmit=提交';
request($url,$postfields,$cookie_jar,'');
unset($postfields,$url);
/*登录论坛 end*/

/*获取帖子列表(位于第一页的帖子) begin*/
$url = 'http://www.eb163.com/forumdisplay.php?fid=57';
$code = request($url,'',$cookie_jar,'');
$threadsList = getThreadsList($code);
/*获取帖子列表 end*/
//帖子序列
$rows = 0;
/*循环抓取所有帖子源代码 begin*/
foreach($threadsList as $list){
$url = "http://www.eb163.com/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*/
?>

作者:YUAN
74478次点击
1个回复  |  直到 2011-12-29 04:31:03
   
  Reply   
阿勒泰姐妹      2011-12-29 04:31:03
沉底了,帮顶。。。。。。。。。购买流量
购买流量就到http://goumaiip.com,购买流量官方网站ip流量源自我平台协议买断的大型门户网站弹窗流量广告,全国范围真实独立流量来路,如果需要测试请少量购买,最低零售:2.99元/1000IP,保证购买流量统计公平公正,IP真实有效,用实力证明一切。
添加一条新回复
您需要登录后才可以回帖 登录 | 成为会员 新浪微博登陆

标签云|手机版|科技先生 ( 京ICP备07036130号 Powered by Discuz! X )

GMT+8, 2024-5-19 02:50