初次接触puppeteer

起因为gitee的page自动部署要付费,所以想着有没有办法进行自动部署,网上有人放出exe工具,属于黑箱子,密码不放心,后来就找到了一篇关于自动部署的方法:
@superYang0033
解决 gitee page 无法自动更新

他提供了一份puppeteer的源码。

但本人常年后端从没接触过puppeteer,不知其。

找到项目网页https://pptr.dev/

从getstart进行了一个简单的试水。后来直接部署上面现有的源码。

注意,使用cnpm可快速下载chrome核心Chromium

#新建文件夹puppeteer
npm init
#回车到结束
npm i puppeteer

将源码保存为index.js到文件夹下,修改登录信息,运行命令

node index.js

则会自动打开浏览器进行自动操作。(可注释掉有头模式)

如此便进行了gitee page的自动化更新。

预想着当我提交commit后,触发gitee调用Jenkins,之后交给部署服务器调用puppeteer,进行自动化更新,岂不美哉!!

直到忽然想到,能登陆的网站都有验证码,或图片或手机验证码,又是一拦路虎,计划图片的话调用图像识别OCR可能会解决,但识别准确率是一大难题。

侥幸心理,一台电脑常常登录的话就可以认为不是机器人,所以再服务器上还是尝试了使用puppeteer,但无法运行:

node example.js 
(node:29187) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
/home/ubuntu/puppeteer/node_modules/_puppeteer@5.2.1@puppeteer/.local-chromium/linux-782078/chrome-linux/chrome: error while loading shared libraries: libXrandr.so.2: cannot open shared object file: No such file or directory


TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md

    at onClose (/home/ubuntu/puppeteer/node_modules/_puppeteer@5.2.1@puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:193:20)
    at Interface.<anonymous> (/home/ubuntu/puppeteer/node_modules/_puppeteer@5.2.1@puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:183:68)
    at Interface.emit (events.js:326:22)
    at Interface.close (readline.js:424:8)
    at Socket.onend (readline.js:202:10)
    at Socket.emit (events.js:326:22)
    at endReadableNT (_stream_readable.js:1226:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:29187) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:29187) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

原来linux运行puppeteer需要很多依赖,windows系统因为有很多自有接口,所以可以使用。linux依赖可以参考:https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md

搜索网上解决方案

  1. 是老老实实安装依赖,然后运行
  2. 运行docker进行环境透明化,依赖交给镜像搞定

事实是第二种可靠,且有人已经上传了可用镜像

教程见:https://zhuanlan.zhihu.com/p/62807598

镜像为pptr-image。

但最后我还是放弃了,因为gitee的验证码是必死的坎,无法绕过,自动部署这个环节很不稳定。就放弃了这个计划。

但windows上的意见部署还是可以做做的,不用自己在打开浏览器进行操作了,自动化就行。

end;