2023年12月7日发(作者:)
node+puppeteer将整个网页html转换为图片并保存【滚动截
屏】
Puppeteer 是 Chrome 开发团队在 2017 年发布的一个 包,用来模拟 Chrome 浏览器的运行。
demo只支持将简单不需要翻页,不需要登陆的页面转换为图片
需要node环境,以及npm或cnpm包管理工具(自行百度)
gitee码云项目地址
开始
进入一个新的项目目录,命令模式进入项目目录
npm init
然后一直回车,内容可不填。
修改 文件为下面的内容:
//
{
"name": "html_to_png",
"version": "1.0.0",
"description": "",
"main": "",
"scripts": {
"start": "node ",
"test": "echo "Error: no test specified" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"puppeteer": "^2.1.1"
}
}
安装依赖
npm install
在项目根目录下创建文件
//
// 引入puppeteer
const puppeteer = require('puppeteer');
// 需要生成的图的文件名
const fileName = '';
// html网络地址
const url = '';
(async () => {
const browser = await ({
defaultViewport: {
width: 1600,
height: 937
},
});
const page = await e();
await (url);
await shot({ path: fileName, fullPage: true });
await ();
})();
运行
npm run start
等待几秒后,如果在项目目录下生成图片,说明成功了。
gitee码云项目地址


发布评论