PHP一个简单的无需刷新爬虫
2019/6/30 16:15:56
本文主要是介绍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一个简单的无需刷新爬虫的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-29env: php: no such file or directory
- 2024-03-01php foreach break
- 2024-02-26Exception参数 php-icode9专业技术文章分享
- 2023-12-30PHP文件批量上传-icode9专业技术文章分享
- 2023-12-30thinkphp6 withJoin-icode9专业技术文章分享
- 2023-12-27MagicArray:像php一样,让Go业务代码不再卷!
- 2023-11-18centos7编译安装PHP教程。
- 2023-11-18centos7编译安装phpMyAdmin教程。
- 2023-10-13唱衰这么多年,PHP 仍然还是你大爷!
- 2023-07-25PHP8,性能更好,语法更好,类型安全更完善