google chrome内核工具Puppeteer

栏目: Node.js · 发布时间: 7年前

内容简介:发表于 2018-09-10 04:52:06 by月小升(一)Puppeteer的简介(二)nodejs的使用

发表于 2018-09-10 04:52:06 by月小升

(一)Puppeteer的简介

(二)nodejs的使用

(三)安装 puppeteer

(四)简单得 puppeteer 开发小例子

(五)Linux系统如何使用

puppeteer

(一)Puppeteer的简介

Puppeteer介绍:

  • 1.可以模拟浏览器来下载
  • 2.可以操纵网页内dom
  • 3.可以跨平台执行
  • 4.表面用的js写的,实际可以在后台运行

官方文档说的很谦虚,可以保存浏览器一个网页为png,pdf,这些都是小case

1.其实你可以用这个软件来开发 爬虫

2.其实你可以用这个软件来自动化测试,写个脚本自动填充表单,自动提交

月小升 今天觉得google的伟大在于技术,技术的作用在于自动化,google从搜索引擎到adwords收费,全部无人值守。用最小的人力完成最多的事情。未来属于机器人时代。

(二)nodejs的使用

简单普及下node.js知识,安装完毕nodejs 你只需要这样执行

a.js

console.log('test');
node a.js

前段工程师不少人对这个很熟悉,作为玩后端的 月小升 ,下载完毕puppeteer直发晕。所以写上面这几句。

(三)安装puppeteer

安装后nodejs系统就有了npm ,如何安装nodejs看上一个博客

npm i puppeteer

(四)简单得puppeteer开发小例子

电脑任意位置写个a.js 内容如下

const puppeteer = require('puppeteer');
 
(async () => {
	const browser = await puppeteer.launch();
	const page = await browser.newPage();
	page.setDefaultNavigationTimeout(12000);
	await page.goto('https://java-er.com');
	//读取java-er.com首页的宽高
	const bodyHandle = await page.$('body');
	const { width, height } = await bodyHandle.boundingBox();
	var heightx = parseInt(height)+1;
	var widthx = parseInt(width)+1;
	console.log(heightx);
	console.log(widthx);
 
	await page.setViewport({
		width: 1920,
		height: heightx
	});
	await bodyHandle.dispose();
	await page.screenshot({path: 'example.png',fullpage:true});
	await browser.close();
})();

命令行执行

node a.js

这样就能把我的首页给存成一个png。

(五)Linux系统如何使用puppeteer

linux 服务器上,我把代码删除的只留一句,都无法执行

const puppeteer = require('puppeteer');
 
(async () => {
	const browser = await puppeteer.launch();
 
})();
TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md
 
    at onClose (/backup/soft/node_modules/puppeteer/lib/Launcher.js:333:14)
    at Interface.helper.addEventListener (/backup/soft/node_modules/puppeteer/lib/Launcher.js:322:50)
    at Interface.emit (events.js:187:15)
    at Interface.close (readline.js:379:8)
    at Socket.onend (readline.js:157:10)
    at Socket.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1092:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
(node:24137) 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(). (rejection id: 1)
(node:24137) [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.

https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md

月小升 猜测这玩意依赖chrome内核

安装chrome 到linux服务器

1.cd /etc/yum.repos.d/

2 vi google.repo

[gogle]
 
name=Google-x86_64
 
baseurl=http://dl.google.com/linux/rpm/stable/x86_64
 
enabled=1
 
gpgcheck=0
 
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub

3.安装google chrome

yum install google-chrome-stable

google chrome 在linux下只能无沙盒执行所以代码要改改

const puppeteer = require('puppeteer');
 
(async () => {
const browser = await puppeteer.launch({args: ['--no-sandbox']});
console.log('OK');
await browser.close();
 
})();

完整的linux 执行代码

const puppeteer = require('puppeteer');
 
(async () => {
const browser = await puppeteer.launch({args: ['--no-sandbox']});
 
const page = await browser.newPage();
	page.setDefaultNavigationTimeout(12000);
	await page.goto('https://java-er.com');
	//读取java-er.com首页的宽高
	const bodyHandle = await page.$('body');
	const { width, height } = await bodyHandle.boundingBox();
	var heightx = parseInt(height)+1;
	var widthx = parseInt(width)+1;
	console.log(heightx);
	console.log(widthx);
 
	await page.setViewport({
		width: 1920,
		height: heightx
	});
	await bodyHandle.dispose();
	await page.screenshot({path: 'example.png',fullpage:true});
	await browser.close();
 
})();

linux上多个example.png

google chrome内核工具Puppeteer

==============================

参考资料:

官方

https://github.com/GoogleChrome/puppeteer

对puppeteer的应用

https://github.com/zhentaoo/puppeteer-deep

中文API手册

https://zhaoqize.github.io/

一个流程测试的案例

https://cnodejs.org/topic/5a041412ad77fa2004549183

首发地址:–

无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Developer's Guide to Social Programming

Developer's Guide to Social Programming

Mark D. Hawker / Addison-Wesley Professional / 2010-8-25 / USD 39.99

In The Developer's Guide to Social Programming, Mark Hawker shows developers how to build applications that integrate with the major social networking sites. Unlike competitive books that focus on a s......一起来看看 《Developer's Guide to Social Programming》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具