让前端飞程序员JavaScript 进阶营

Puppeteer初探

2019-01-21  本文已影响54人  tiancai啊呆

是什么

Puppeteer 是一个Node库,支持调用Chrome的API来操纵Web。Chrome团队在维护,会拥有更好的兼容性和前景。

用途

demo

const puppeteer = require('puppeteer');

async function testJuejinUi() {
  try {
    const browser = await puppeteer.launch({
      headless: false
    });
    const page = await browser.newPage();
    const dimensions = await page.evaluate(() => {
      return {
        width: window.outerWidth,
        height: window.outerHeight,
      };
    });
    await page.setViewport({
      width: dimensions.width,
      height: dimensions.height,
    })
    await page.goto('https://juejin.im/timeline');
    await page.waitFor(1000)
    await page.screenshot({ path: 'juejin-timeline.png' });
    await page.tap('.nav-list>li:nth-child(2)>span')
    await page.waitFor(1000)
    await page.screenshot({ path: 'juejin-timeline-frontend.png' });
    await page.type('.search-form>.search-input', 'puppeteer', { delay: 200 })
    await page.tap('.search-form>.search-icon')
    await page.waitFor(1000)
    await page.screenshot({ path: 'juejin-search.png' });
    await browser.close();
  } catch (error) {
    console.log(error)
  }
}
testJuejinUi()
const puppeteer = require('puppeteer');

async function testJuejinPerformance() {
  try {
    const browser = await puppeteer.launch({ headless: false });
    const page = await browser.newPage();
    const dimensions = await page.evaluate(() => {
      return {
        width: window.outerWidth,
        height: window.outerHeight,
      };
    });
    await page.setViewport({
      width: dimensions.width,
      height: dimensions.height,
    })
    await page.tracing.start({ path: 'trace.json' });
    await page.goto('https://juejin.im/timeline');
    await page.tracing.stop();
    await browser.close();
  } catch (error) {
    console.log(error)
  }
}
testJuejinPerformance()
const puppeteer = require('puppeteer');
const fs = require('fs');
async function testJuejinUi() {
  try {
    const browser = await puppeteer.launch({
      headless: false
    });
    const page = await browser.newPage();
    const dimensions = await page.evaluate(() => {
      return {
        width: window.outerWidth,
        height: window.outerHeight,
      };
    });
    await page.setViewport({
      width: dimensions.width,
      height: dimensions.height,
    })
    await page.goto('https://juejin.im/timeline');
    await page.waitFor(1000)
    let arr = await page.$$('ul.entry-list .entry-box')
    arr = Array.from(arr)
    const result = await Promise.all(arr.map(async (ele) => {
      const title = await ele.$eval('.title-row a.title', (dom) => {
        return dom.innerText
      })
      const author = await ele.$eval('li.username a', (dom) => {
        return dom.innerText
      })
      return title + '--' + author
    }))
    fs.writeFileSync('1.text', '')
    result.forEach((ele) => {
      fs.appendFileSync('1.text', ele + '\n')
    })
    await browser.close();
  } catch (error) {
    console.log(error)
  }
}
testJuejinUi()

常用API

const puppeteer = require('puppeteer');
const browser = puppeteer.launch()    // 创建浏览器实例
const page = await browser.newPage();   // 打开新页面
await page.goto('https://juejin.im/timeline');  // 新页面地址
await page.screenshot({ path: 'juejin-timeline.png' });  // 截图功能
await page.waitFor(1000)  // 等待1秒
await browser.close();  // 关闭浏览器
await page.tap('.search-form>.search-icon')  // 点击指定元素
await page.$(selector)  // 等同在页面内执行 document.querySelector
await page.$$(selector)  // 等同在页面内执行 document. querySelectorAll
await page.evaluate(pageFunction[, ...args])  // 在页面实例上下文中执行js函数

参考资料

Puppeteer中文文档
Puppeteer github

上一篇下一篇

猜你喜欢

热点阅读