php 論壇採集程式 類比登陸,抓取頁面 實現代碼

來源:互聯網
上載者:User

複製代碼 代碼如下:<?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*/
?>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.