之前采集的时候会遇到页面是用ajax渲染的,如果还是使用之前的方式采集就会采集不到,必须通过解析ajax代码来抓取内容。下面提供一种方式使用PhantomJS来做

安装composer
如果你没有安装可以参考之前的文章【推荐阅读:Walle-瓦力上线部署系统正确安装https://www.phpsong.com/2166.html】 这里不讲述

使用 Composer 安装
我这里安装到/home/wwwroot/querylist/目录下

  1. cd /home/wwwroot/querylist/

#安装 QueryList

  1. composer require jaeger/querylist

#安装 PhantomJS 插件

  1. composer require jaeger/querylist-phantomjs

下载 PhantomJS 二进制文件
官方下载地址: http://phantomjs.org/download.html
我这里是linux系统
#下载phantomjs

  1. wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2

#解压

  1. tar -jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2

#把解压的phantomjs复制到querylist目录里

  1. cp -r phantomjs-2.1.1-linux-x86_64 /home/wwwroot/querylist/

开始采集
#新建一个文件

  1. touch /home/wwwroot/querylist/index.php

文件内容

  1. <?php
  2. include './vendor/autoload.php';
  3.  
  4. use QL\QueryList;
  5. use QL\Ext\PhantomJs;
  6.  
  7. $ql = QueryList::getInstance();
  8. //设置 PhantomJS运行文件在bin目录下
  9. $ql->use(PhantomJs::class,'/home/wwwroot/querylist/phantomjs-2.1.1-linux-x86_64/bin/phantomjs');
  10.  
  11. $html = $ql->browser('http://www.phpsong.com')->getHtml();
  12. print_r($html);

ps.php要把proc_open函数放开,不然会提示错误

  1. PHP Warning: proc_open() has been disabled for security reasons in /home/wwwroot/querylist/vendor/jonnyw/php-phantomjs/src/JonnyW/PhantomJs/Procedure/Procedure.php on line 107
  2. PHP Fatal error: Uncaught JonnyW\PhantomJs\Exception\ProcedureFailedException: Error when executing PhantomJs procedure - proc_open() did not return a resource in /home/wwwroot/querylist/vendor/jonnyw/php-phantomjs/src/JonnyW/PhantomJs/Procedure/Procedure.php:138

目前有部分网站针对phantomjs不让采集了,比如今日头条

相关评论(0)
您是不是忘了说点什么?

友情提示:垃圾评论一律封号...

还没有评论,快来抢沙发吧!