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

前端之家收集整理的这篇文章主要介绍了php 论坛采集程序 模拟登陆,抓取页面 实现代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="codetitle"><a style="CURSOR: pointer" data="78994" class="copybut" id="copybut78994" onclick="doCopy('code78994')"> 代码如下:

<div class="codebody" id="code78994">
<?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>/',$error);
return isset($error[0])?false:true;
}
//获取帖子标题
function getTitle($code){
preg_match('/

[^ <\/h1>]/',$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)\">.+/',$author_tmp);
$author = strip_tags($author_tmp[0]);
return $author;
}
//获取楼主发表的内容
function getContents($code){
pregmatch('/ <div id=\"postmessage\d+\" class=\"t_msgfont\">(.|\r|\n)? <\/div>/',$contents_tmp);
$contents = preg_replace('/images\//','http://bbs.war3.cn/images/',$contents_tmp[0]);
return $contents;
}
//打印帖子标题
function printTitle($title){
echo "

帖子标题:

",strip_tags($title),"

";
}
//输出帖子作者
function printAuthor($author){
echo "

帖子作者:

",strip_tags($author),"

";
}
//打印帖子内容
function printContents($contents){
echo "

作者发表的内容:

",$contents,"
";
}
//错误
function printError(){
echo " 该帖子不存在! ";
}
/
函数列表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,'');
unset($postfields,$url);
/
登录论坛 end/
/
获取帖子列表(位于第一页的帖子) begin/
$url = 'http://bbs.war3.cn/forumdisplay.PHP?fid=57';
$code = request($url,'','');
$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,'');
$color = $rows%2==0?'#00CCFF':'#FFFF33';
echo "
";
echo "

第",($rows+1),"贴:


";
$author = getAuthor($code);
printAuthor($author);
$title = getTitle($code);
printTitle($title);
$contents = getContents($code);
printContents($contents);
echo "
";
$rows++;
}
else
printError();
echo "-----------------------------------------------------------------------------------------

";
}
/
抓取源代码 end*/
?>

原文链接:https://www.f2er.com/php/29132.html

猜你在找的PHP相关文章