What can I do?

Most things that you can do manually in the browser can be done using Puppeteer! Here are a few examples to get you started:

  1. Generate screenshots and PDFs of pages.
  2. Crawl a SPA and generate pre-rendered content (i.e. "SSR").
  3. Automate form submission, UI testing, keyboard input, etc.
  4. Create an up-to-date, automated testing environment. Run your tests directly in the latest version of Chrome using the latest JavaScript and browser features.
  5. Capture a timeline trace of your site to help diagnose performance issues.

例子说明:

const puppeteer = require('puppeteer'); // 导入puppeteer库
(async () => { //固定语法格式
const browser = await puppeteer.launch(); //根据puppeteer创建一个Browser对象,相当于启动了浏览器
//const browser = await puppeteer.launch({headless:false}); //相当于以可视的方式启动了浏览器,headless默认为true
//const browser = await puppeteer.launch({executablePath:'/Volumes/A/chrome'});//引用其它谷歌chrome版本,但兼容性很差,只适用于Chrome Dev或Chrome Canary
const page = await browser.newPage(); //根据Browser创建一个Page对象,相当于打开一个标签页
await page.goto('https://example.com'); //page.goto()跳转到指定url地址
await page.screenshot({path: 'example.png'}); //page.screenshot()对页面进行截图
await browser.close(); //关闭浏览器
})();

// Example1 - navigating to https://example.com and saving a screenshot as example.png.截取网页为png图片

const puppeteer = require("puppeteer");
(async () =>{
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path:'example.png'});
await browser.close();
})();

// Example2 - create a PDF.将网页生存PDF格式文档

const puppeteer = require('puppeteer');

(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
//await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle2'});
await page.goto('https://www.baidu.com', {waitUntil: 'networkidle2'});
await page.pdf({path: 'hn0.pdf', format: 'A4'}); await browser.close();
})();

//Example3 - evaluate script in the context of the page,打印网页信息

const puppeteer = require('puppeteer');

(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com'); // Get the "viewport" of the page, as reported by the page.
const dimensions = await page.evaluate(() => {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
deviceScaleFactor: window.devicePixelRatio
};
}); console.log('Dimensions:', dimensions); await browser.close();
})();

puppeteerExamples的更多相关文章

随机推荐

  1. SpringBoot 原理分析、监控、项目部署

    目录 SpringBoot 监控 概述 使用 SpringBoot Admin 概述 使用 SpringBoot 项目部署 SpringBoot 监控 概述 SpringBoot 自带监控功能 Act ...

  2. Python_使用smtplib+email完成邮件发送

    本文以第三方QQ邮箱服务器演示如何使用python的smtplib+email完成邮箱发送功能 一.设置开启SMTP服务并获取授权码 开启QQ邮箱SMTP服务 开启的最后一步是发送短信验证,获取 au ...

  3. spring boot 解决 跨域 的两种方法 -- 前后端分离

    1.前言 以前做项目 ,基本上是使用 MVC 模式 ,使得视图与模型绑定 ,前后端地址与端口都一样 , 但是现在有些需求 ,需要暴露给外网访问 ,那么这就出现了个跨域问题 ,与同源原则冲突, 造成访问 ...

  4. k8s中的nginx-ingress如何配置路径重定向

    k8s中的nginx-ingress如何配置路径重定向 一. 需求描述 路径重定向的一般应用场景: 调整用户浏览的URL,看起来更规范 为了让搜索引擎收录网站内容,让用户体验更好 网站更换新域名后 根 ...

  5. 通过暗码去打开/关闭usb debug开关

    通过暗码去打开/关闭usb debug开关 通过暗码去打开/关闭usb debug开关1. Description2. Analysis3. Solution4. Summary 1. Descrip ...

  6. X-Forwarded-for漏洞解析

    首先了解X-Forwarded-for(简称:XFF) X-Forwarded-for:简称XFF,它代表客户端,也就是HTTP的请求真实的IP,只有在通过了HTTP代理或者负载均衡器时才会添加该项. ...

  7. deepin20搜狗输入法使用

    放大打字框 打出中文语句符号

  8. Ajax_Post用法

    Ajax_Post用法 post方法的用法其实跟get是大同小异的 唯一不同的地方就是我们需要修改server.js的文件 只需要将get修改为post即可 那么我为了方便操作我这里选择的是直接在下面 ...

  9. 加深对AQS原理的理解示例二:自己设计一个同步工具,同一时刻最多只有两个线程能访问,超过线程将被阻塞

    /** *@Desc 设计一个同步工具,同一时刻最多只有两个线程能访问,超过线程将被阻塞<br> * 思路分析: * 1.共享锁 两个线程及以内能成功获取到锁 * 2. *@Author ...

  10. python操作MySQL--实例

    有一段时间,没更新,但这都不是自己懒惰的借口,希望自己能坚持下去: python可以操作MySQL 数据库,需要安装的是MySQL-python,并且实现读入excel表及生成excel 表,则需要x ...