您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
使用PHP怎么实现一个无需刷新的爬虫?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
<?php
//设置最大执行时间
set_time_limit(0);
function getHtml($url){
// 1. 初始化
$ch = curl_init();
// 2. 设置选项,包括URL
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADER,0);
// 3. 执行并获取HTML文档内容
$output = curl_exec($ch);
if($output === FALSE ){
$output = '';
}
// 4. 释放curl句柄
curl_close($ch);
return $output;
}
function getPageData($url){
// 获取整个网页内容
$html = getHtml($url);
// 初步获取主块内容
preg_match("/教程列表.*教程列表/s",$html,$body_html);
// 返回数据
$data = array();
//判断是否存在要获取的内容
if(count($body_html)){
// 获取页面指定信息
preg_match_all('/<a class="avatar".*user_id="(\S*)" href="(\S*)" rel="external nofollow" /',$body_html[0],$info_1);
preg_match_all('/<a href="(.*)" rel="external nofollow" .*title="(.*)"/',$body_html[0],$info_2);
$info = array_merge($info_1,$info_2);
//组合的信息
for($index=0; $index<count($info[0]); $index++){
//以文章信息作为key存数组,以及覆盖旧数据
$data[$info[4][$index]] = array(
'user_id' => $info[1][$index],
'user_home' => $info[2][$index],
'a_url' => $info[4][$index],
'a_title' => $info[5][$index],
);
}
}
return $data;
}
header("Content-type: text/html; charset=utf-8");
echo '<pre>';
// 初始化数据
$page_no = 1;
$data_all = array();
// 分页获取数据
do{
$url = 'http://www.thinkphp.cn/code/examples/p/' . $page_no;
$data = getPageData($url);
$data_all += $data;
$page_no ++;
}while ($page_no <= 10); //当前只获取10页,如果要全部获取则把条件换成$data或!empty($data)
var_dump($data_all);
?>看完上述内容,你们掌握使用PHP怎么实现一个无需刷新的爬虫的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。