注意,资料来源网络,非原创,我是搬运工、测试工。
1,先安装宝塔
2,在宝塔的软件商店找到pm2并安装
3,安装淘宝NPM镜像,
npm install -g cnpm --registry=https://registry.npm.taobao.org
4,在/www/wwwroot 下面创建一个文件夹:mkdir onenode
5,cd /www/wwwroot/onenode
6,npm init
7, 安装 npm i puppeteer
8, 安装puppeteer后,检查puppeteer依赖是否全部安装,执行:
ldd node_modules/puppeteer/.local-chromium/linux-555668/chrome-linux/chrome
注意,如果上面这个命令无效,就根据第13步看到的目录路径,cd进去,然后执行:ldd chrome | grep not 查看缺失的包
9, 发现未安装依赖,将每一个都执行一次命令查看其依赖包,记得每一个都要执行一次:
repoquery --nvr --whatprovides xxx.xxx.xx
10, 执行上一步后,会弹出对应的依赖包,然后复制粘贴,安装此依赖: yum install ******
11,创建app.js
const puppeteer = require(‘puppeteer’); const { targetPath } =
require(’./storage.config’)try {
(async () => {
const browser = await puppeteer.launch(); //打开浏览器
const page = await browser.newPage(); //新建标签页
await page.goto(‘https://www.baidu.com’); //跳转指定页面
await page.screenshot({ //截图 默认800x600
path:${targetPath}/home.png
});
await browser.close();//关闭浏览器
console.log(‘截图成功’);
})(); } catch (error) {
console.log(error.message); }
12, 创建storage.config.js
// 该文件用于配置截取图片保存的路径
const path = require(‘path’); const fs = require(‘fs’);
// 设置保存图片路径为根目录下的img文件夹 const targetPath = path.resolve(__dirname,
‘img’);//如果该文件夹不存在,则创建
if (!fs.existsSync(targetPath)) {
fs.mkdirSync(targetPath) }exports.targetPath=targetPath
13, 执行 node app.js, 如果出现以下错误:
就请你重复第8步,检查是否依赖安装完毕。
补充:中文乱码问题
1.安装fontconfig(如系统自带,可以不装):
yum -y install fontconfig
2.添加中文字体库
从window的C:\Windows\Fonts里面把你需要的字体拷贝出来。比如simfang.ttf
在CentOS的/usr/share/fonts新建一个叫chinese的文件夹
然后把刚刚拷贝字体放到CentOS的/usr/share/fonts/chinese里面作者:Alinawu 链接:https://www.jianshu.com/p/f2ba4f5b8f36 来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
3.修改chinese目录的权限:
chmod -R 775 /usr/share/fonts/chinese
4.接下来需要安装ttmkfdir来搜索目录中所有的字体信息,并汇总生成fonts.scale文件,输入命令
yum -y install ttmkfdir
ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir
5.修改字体配置文件 vi /etc/fonts/fonts.conf
<dir>/usr/share/fonts</dir>
<dir>/usr/share/X11/fonts/Type1</dir>
<dir>/usr/share/X11/fonts/TTF</dir>
<dir>/usr/local/share/fonts</dir>
<dir>/usr/local/share/fonts/chinese</dir>
<dir prefix="xdg">fonts</dir>
<!-- the following element will be removed in the future -->
<dir>~/.fonts</dir>
6.刷新内存中的字体缓存,命令行输入:
fc-cache
7.看一下现在机器上已经有了刚才添加的字体。命令行输入:
fc-list :lang=zh
8.再运行一下node app.js截图,完毕。
文章参考连接:
1.https://blog.csdn.net/qq_42813491/article/details/102645851
2.https://www.cnblogs.com/xuzhengzong/articles/14135344.html
3.https://www.jianshu.com/p/f2ba4f5b8f36
4.https://www.jianshu.com/p/163d355d2454?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation
5.https://www.bwmelon.com/archives/27/