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. 第10组 Alpha冲刺 (1/6)

    1.1基本情况 ·队名:今晚不睡觉 ·组长博客:https://www.cnblogs.com/cpandbb/ ·作业博客:https://edu.cnblogs.com/campus/fzu/FZ ...

  2. Pandas系列(十八)- 多级索引

    多级索引 多级索引(也称层次化索引)是pandas的重要功能,可以在Series.DataFrame对象上拥有2个以及2个以上的索引.实质上,单级索引对应Index对象,多级索引对应MultiInde ...

  3. RabbitMQ除开RPC的五种消模型----原生API

    2.五种消息模型 RabbitMQ提供了6种消息模型,但是第6种其实是RPC,并不是MQ,因此不予学习.那么也就剩下5种. 但是其实3.4.5这三种都属于订阅模型,只不过进行路由的方式不同. 通过一个 ...

  4. sql审核-避免离线sql导致的db集群故障

    关键词: sql审核.sql审批.sql检查.sql检测.sql执行 离线sql可能会导致的问题 首先,什么是离线sql呢?就是说手动触发执行的这种sql:相对的还有在线sql,位于我们的程序代码中, ...

  5. 问题记录——BigDecimal保留两位小数及格式化成百分比

    1.函数总结 BigDecimal.setScale()方法用于格式化小数点 setScale(1)表示保留一位小数,默认用四舍五入方式 setScale(1,BigDecimal.ROUND_DOW ...

  6. 【记录一个问题】linux下使用opencv中的UMat,性能并未提升,反而略有下降

    使用后性能略微下降,一开始怀疑是UMat拷贝的问题.运行 nvidia-smi -l 1, 发现GPU占用始终为0.说明opencl使用的是CPU版本,而不是GPU版本.明天试验opencl的GPU版 ...

  7. 开源办公套件DzzOffice安装教程

    DzzOffice开源办公套件 DzzOffice是一套开源办公套件,适用于企业.团队搭建自己的 类似"Google企业应用套件"."微软Office365"的 ...

  8. shell循环ping ip的写法

    #!/bin/bash for i in `seq 1 20` do if ping -w 2 -c 1 192.168.43.$i | grep "100%" > /dev ...

  9. java解洛谷P1011车站问题

    车站每站的上车人数,下车人数,剩余人数都组成了斐波那契数列 此代码只计算了剩余人数的情况,所以在输入需要总站数量时会-1取上一站的剩余人数 (最后一站会全部下车,没有上车人数) 每一站的剩余人数都可以 ...

  10. 010 Linux 文本统计与去重 (wc 和 uniq)

    wc 命令一般是作为组合命令的一员与其他命令一同起到统计的作用.而一般情况下使用wc -l 命令较多. uniq 可检查文本文件中重复出现的行,一般与 sort 命令结合使用.一起组合搭配使用完成统计 ...