美文网首页
PHP 爬虫

PHP 爬虫

作者: 南哥教你编程 | 来源:发表于2017-07-11 16:20 被阅读0次

昨天面试萌宝的php实习,给了我一个爬虫的题目,瞬间懵逼,我的天,从来没接触过爬虫啊。于是简单的了解了下,决定用html-parse来解析html_dom,因为真心对正则表达式感到害怕。
然而最后还是被pass了,说小公司,我的能力达不到...建议我去大公司。。。果然还是大公司比较好进是吧?


    public function start(){
      //获取链接数组
      $this->setLinksArray();
      $movies = [];
      foreach($this->linksArray as $link){
                $html_dom = $this->getContentByFilegetContent($link);
              // 找出items
              $ol = $html_dom->find('ol.grid_view',0);
              $items = $ol->find('li');
              // 获取基础信息
              foreach ($items as $item){
                  $em = $item->find('em',0)->getPlainText();
                  $title = $item->find('span.title',0)->getPlainText();
                  $img = $item->find('img',0)->getAttr('src');
                  $inq = $item->find('span.inq',0);
                  if($inq == false){
                    $inq = '';
                  }else{
                    $inq=$inq->getPlainText();
                  }
                  $rating_num = $item->find('span.rating_num',0)->getPlainText();
                  $ob = [$em => [
                    'title'=>$title,
                    'img'=>$img,
                    'inq'=>$inq,
                    'rateing_num'=>$rating_num
                  ]];
                  array_push($movies,$ob);
                  // 获取更多信息
                  // $moreUrl = $item->find('div.hd',0)->find('a',0)->getAttr('href');
                  // if($this->isUrlValid($moreUrl)){
                  //   echo $moreUrl;
                  //     $moreInfo_dom = $this->getContentByFilegetContent($item->find('div.hd',0)->find('a',0)->getAttr('href'));
                  //     $info_dom = $moreInfo_dom->find('info',0);
                  //     $director = $info_dom->find('span',0)->find('a',0)->getPlainText();
                  //     $bianju = $info_dom->find('span',1)->find('a',0)->getPlainText();
                  //     // 获取moreInfo(导演,主演,编剧) 测试阶段被墙了
                  // }
              }
      }
      //将json写入文件
      file_put_contents("/Users/z/Code/test.txt",json_encode($movies));
    }
    // 设置linksArray
    public function setLinksArray(){
      $html_dom = $this->getContentByFilegetContent(self:::URL);
      //找出翻页div
      $div_paginator =  $html_dom->find('div.paginator',0);
      $linksuffixs = $div_paginator->find('a');
      //250条数据的链接数组
      $this->linksArray = array_map(function ($linksuffix){
          return self::URL.$linksuffix->getAttr('href');
      },$linksuffixs);

      array_pop($this->linksArray);
      array_unshift($this->linksArray,self::URL);
    }
    //根据url,获取dom对象
    public function getContentByFilegetContent($url){
      $content = file_get_contents($url);
      $html_dom = new \HtmlParser\ParserDom($content);
      return $html_dom;
    }
    //curl 请求
    header("Content-type:text/html;charset=utf-8");
    function GetCurl($url){
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_NOBODY,true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
        curl_setopt($ch, CURLOPT_AUTOREFERER,true);
        curl_setopt($ch, CURLOPT_TIMEOUT,30);
        $rtn = curl_exec($ch);
        curl_exec($ch);
        return $rtn;
    }// 判断url是否有效
    public function isUrlValid($url){
        $resp = $this->GetCurl($url);
        if(strpos($resp,'404 Not Found')==true) {
          return false;
        }else{
          return true;
        }
    }


}
` ` `

相关文章

  • 网络爬虫1--http协议和urllib

    爬虫初步 爬虫概念 都有哪些语言可以实现爬虫 ​ (1)php, 号称世界上最好的语言,可以实现爬虫,但做的不好...

  • Goutte基本用法

    最近工作上用到PHP爬虫框架Goutte(号称是PHP上最好用的爬虫框架)。这里记下自己用到过的使用技巧,免得下次...

  • phpspider简单快速上手的php爬虫框架

    前言 前段时间接到一个开发采集网站数据的项目,从事php开发的我立刻想到使用php做爬虫。虽然python爬虫方便...

  • 浅谈爬虫

    1.什么是爬虫? 爬虫:就是抓取网页中的数据 2.为什么选择python做爬虫? 可以做爬虫的语言有很多,如PHP...

  • PHP 爬虫

    昨天面试萌宝的php实习,给了我一个爬虫的题目,瞬间懵逼,我的天,从来没接触过爬虫啊。于是简单的了解了下,决定用h...

  • php爬虫

    php爬虫可以用phpspider、querylisthttps://doc.phpspider.orghttp:...

  • PHPspider爬虫10分钟快速教程

    说到做爬虫,大家都可能第一时间想到的是python,其实php也是可以用来写爬虫程序的。php一贯简洁、易用,亲测...

  • 关于python爬虫

    最近在弄一个需求,需要写到爬虫,本来是使用php的,但是php没有一款可以满足需要的爬虫框架,于是转而使用pyth...

  • 爬虫框架整理汇总

    整理了Node.js、PHP、Go、JAVA、Ruby、Python等语言的爬虫框架。不知道读者们都用过什么爬虫框...

  • PHP网络爬虫

    目前网络爬虫用的比较多的是用Python写的,有很多第三方包可以直接使用,而使用PHP的网络爬虫很少有现成的开发包...

网友评论

      本文标题:PHP 爬虫

      本文链接:https://www.haomeiwen.com/subject/fmjmhxtx.html