实现代码如下:
<?php /** * php curl采集Discuz * www.jb200.com */ set_time_limit(0); //cookie保存目录 $cookdir = './cookie.tmp'; //模拟请求数据 Function request($url,$action,$cookdir,$referer){ $ch = curl_init(); $options = array(CURLOPT_URL => $url, CURLOPT_HEADER => 0, CURLOPT_NOBODY => 0, CURLOPT_PORT => 80, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $action, CURLOPT_RETURNTRANSFER => 1, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_COOKIEJAR => $cookdir, CURLOPT_COOKIEFILE => $cookdir, CURLOPT_REFERER => $referer ); curl_setopt_array($ch, $options); $code = curl_exec($ch); curl_close($ch); return $code; } //获取帖子列表 Function getList($code){ //<a href="viewthread.php?tid=16&extra=page%3D1">加打塔洗花腳本</a> preg_match_all('/<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; } //登录论坛/ $url = 'http://www.jb200.com/logging.php?action=login'; $action='loginfield=username&username=see7di&password=fjin999&questionid=0&cookietime=315360000&referer=http://bbs.war3.cn/&loginsubmit=提交'; request($url,$action,$cookdir,''); unset($action,$url); //获取帖子列表(位于第一页的帖子) $url = 'http://www.jb200.com/forumdisplay.php?fid=5'; $code = request($url,'',$cookdir,''); $tList = getList($code); //循环抓取每個帖子內的標題 foreach($tList as $list){ $url = "http://www.jb200.com/viewthread.php?tid={$list}"; $code = request($url,'',$cookdir,''); if(isExits($code)){ $title = getTitle($code); echo "tid:{$list}:",strip_tags($title),"<br>"; }else{ echo "tid:{$list}:该帖子不存在!<br>"; } } ?>