• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    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; $indexcount($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远程请求CURL实例教程(爬虫、保存登录状态)
    • PHPCrawl爬虫库实现抓取酷狗歌单的方法示例
    • php与python实现的线程池多线程爬虫功能示例
    • 利用php抓取蜘蛛爬虫痕迹的示例代码
    • PHP实现爬虫爬取图片代码实例
    上一篇:PHP智能识别收货地址信息实例
    下一篇:PHP parse_ini_file函数的应用与扩展操作示例
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    PHP一个简单的无需刷新爬虫 PHP,一个,简单,的,无需,刷新,