PHP使用CURL抓取网页并将网页保存为图片

PHP使用CURL抓取网页并将网页保存为图片。
使用CURL抓取到的网页都是html代码。如何实现和浏览器相同的功能,抓取网页后能像浏览器渲染后的效果,并保存为图片保存到服务器(Linux)。

阅读 12.5k
4 个回答

使用php调用phantomjs

npm install phantomjs -g
vi test.js
var webPage = require('webpage');
var page = webPage.create();

page.viewportSize = { width: 1920, height: 1080 };
page.open("http://www.baidu.com", function start(status) {
  page.render('baidu.jpg', {format: 'jpeg', quality: '100'});
  phantom.exit();
});

//php
shell_exec('phantomjs test.js')

使用wkhtmltopdf
javascript可以使用html2canvas处理

php 是不行的, 可以用这个 CutyCapt.exe

因为 php 用 exec() 函数是可以执行 终端命令的

下载 CutyCapt.exe : https://pan.baidu.com/s/1jIgANA2

这里的代码 用的 别人的,不知道能不能用,这种代码没必要自己写,理解思路很重要

<?php

//设置页面编码
header("Content-Type:text/html; charset=utf-8");

//设置运行不超时
set_time_limit(0);

//抓取网址
$url="https://segmentfault.com/q/1010000007909398"; 

//设置图片名称
$time=time();  

//设置图片输出地址
$outdir = 'E:/BaseServer2/htdocs/curlImg/'.$time.".png";   

//CutyCapt绝对路径
$path = 'E:/BaseServer2/htdocs/CutyCapt.exe';

//命令
$cmd = "$path --url=$url --out=$outdir"; 

//执行命令
exec($cmd); 
echo  "图片抓取成功!";

如果用 直接输出,我只用过 保存为world文档形式的:

参考这个:https://github.com/lmxdawn/th...

这是我之前 用到过的

 <?php
// +----------------------------------------------------------------------
// | lmxdawn [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 .
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Byron Sampson <lmxdawn@gmail.com>
// +----------------------------------------------------------------------
/**
 * 把资源强制变为doc文档形式
 * Class TestImages
 */
class World{
    private  $config = array(
    );
    //保存例实例在此属性中
    private static $_instance;
    /**
     * Curl constructor.私有构造函数,防止外界实例化对象
     * @param array $config 配置
     */
    private function __construct($config = array()) {
        if (!empty($config) && is_array($config)){
            $this->config = array_merge($this->config,$config);
        }
    }
    /**
     * 私有克隆函数,防止外办克隆对象
     */
    private function __clone() {}
    /**
     * 静态方法,单例统一访问入口
     * @param array $config 配置
     * @return TestImages
     */
    public static function getInstance($config = array()) {
        if (is_null ( self::$_instance ) || isset ( self::$_instance )) {
            self::$_instance = new self ($config);
        }
        return self::$_instance;
    }
    /**
     * 保存为图片
     * @param $html 页面数据
     * @param $name
     */
    public function down($html,$name)
    {
        $image_path ="./".$name.".doc";
        // 把文件写入磁盘
        $this->wirtefile($image_path,$html);
    }
    private function wirtefile ($fn,$data){
        $fp=fopen($fn,"wb");
        fwrite($fp,$data);
        fclose($fp);
    }
}

    // 载入 curl 请求类
    require './src/Curl.php';
    header("Content-type:text/html;charset=utf-8");
    $curl = lmxdawn\curl\Curl::getInstance();
    $html_data = $curl->send_http('https://segmentfault.com/','get');
    //var_dump($html_data);
    $image = World::getInstance();
    $image->down($html_data,'test');

phantomjs 可以网页截图。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题