博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
puppeteer api_如何使用Node.js和Puppeteer API(1)截屏?
阅读量:2544 次
发布时间:2019-05-11

本文共 2940 字,大约阅读时间需要 9 分钟。

puppeteer api

In my last articles, I ended on how we can create PDF files using Node.js and puppeteer. We saw the various ways how we can create PDF files using Node.js and Puppeteer. As we already know, Puppeteer is a Node library developed by Google and provides a high-level API for developers.

在上一篇文章中,我结束了如何使用Node.js和puppeteer创建PDF文件的内容。 我们看到了使用Node.js和Puppeteer创建PDF文件的各种方法。 众所周知,Puppeteer是Google开发的Node库,并为开发人员提供了高级API。

Today, we will learn the first method of how we can take a screenshot (screen capture) of a web page/website?

今天,我们将学习如何获取网页/网站的屏幕快照(屏幕截图)第一种方法

Note: You should have Node.js and puppeteer installed in your PC.

注意:您应该在PC中安装Node.js和puppeteer。

With Node.js and puppeteer already up and running, let's get started.

现在已经启动并运行了Node.js和puppeteer,让我们开始吧。

Open a text editor and type the following code and save it with the file name app.js:

打开文本编辑器,输入以下代码,并将其保存为文件名app.js:

const puppeteer = require('puppeteer'); // loads the puppeteer module(async () => {
const browser = await puppeteer.launch() // opens a virtual browser const page = await browser.newPage() // creates a new page // you can also set dimensions await page.setViewport({
width: 3000, height: 100}) // sets it's dimensions await page.goto('file:///E:/HDD%2080%20GB%20DATA/CODING%20TUTORIALS%20AND%20CODES/go237.com/go237%20web/New%20design/index.html') // navigates to the url // NB: You can use any url of your choice, // I this example, I used a webpage that was already downloaded to facilitate the tutorial //but if it concerns a url on the www , internet connection is required to navigate to the website/web page await page.screenshot({
path: 'myscreenshot.png', fullPage: true }) // takes a screenshot console.log ('done'); // console message when conversion is complete! await browser.close() // closes the browser.})()

The file should be saved in your Node.js directory.

该文件应保存在您的Node.js目录中。

From the code above, we first of all include the puppeteer module,

从上面的代码中,我们首先包括puppeteer模块,

  • Launch a virtual browser

    启动虚拟浏览器

  • Open a new page with it's dimensions

    用尺寸打开一个新页面

  • Navigate to the url address

    导航到URL地址

  • Take a screenshot and then finally close the browser

    截屏,然后最后关闭浏览器

To convert the screenshot to PDF you can add this code before the line console.log ('done');

要将屏幕截图转换为PDF,您可以在console.log('done');行之前添加此代码

await page.pdf ({path: 'myscreenshot.pdf', format: 'A4' });

You can convert to other sizes like; letter, tabloid, A1, A2, A3, A4

您可以转换成其他尺寸,例如; 字母,小报,A1,A2,A3,A4

Run the code by initiating the file at the command prompt like a regular Node.js file.

通过在命令提示符处启动文件(如常规Node.js文件)来运行代码。

Following our code, done will be printed out on the command line console when the conversion is complete.

遵循我们的代码,转换完成后, done将打印在命令行控制台上。

take-screenshot-1-0

The Output image file is then stored in the default Node.js directory with name myscreenshot.png.

然后,将输出图像文件存储在默认的Node.js目录中,其名称为myscreenshot.png

Output file:

输出文件:

take-screenshot-1-1

翻译自:

puppeteer api

转载地址:http://cbazd.baihongyu.com/

你可能感兴趣的文章
STM32+IAP方案 实现网络升级应用固件
查看>>
用74HC165读8个按键状态
查看>>
jpg转bmp(使用libjpeg)
查看>>
linear-gradient常用实现效果
查看>>
sql语言的一大类 DML 数据的操纵语言
查看>>
VMware黑屏解决方法
查看>>
JS中各种跳转解析
查看>>
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>
H5 表单标签
查看>>
su 与 su - 区别
查看>>
C语言编程-9_4 字符统计
查看>>
在webconfig中写好连接后,在程序中如何调用?
查看>>