php 浏览器 保存网页图片,PHP使用CURL抓取网页并将网页保存为图片

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

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

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

//设置页面编码

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文档形式的:

这是我之前 用到过的

// +----------------------------------------------------------------------

// | lmxdawn [ WE CAN DO IT JUST THINK IT ]

// +----------------------------------------------------------------------

// | Copyright (c) 2016 .

// +----------------------------------------------------------------------

// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )

// +----------------------------------------------------------------------

// | Author: Byron Sampson

// +----------------------------------------------------------------------

/**

* 把资源强制变为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');

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值