Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

puppeteer 学习笔记 #13

Open
maomao1996 opened this issue Oct 22, 2020 · 0 comments
Open

puppeteer 学习笔记 #13

maomao1996 opened this issue Oct 22, 2020 · 0 comments

Comments

@maomao1996
Copy link
Owner

maomao1996 commented Oct 22, 2020

puppeteer 学习笔记

安装

yarn add puppeteer
# OR
yarn add puppeteer-core

puppeteer-core 版本不会额外下载一个 Chromium (安装贼慢),同时会忽略所有 PUPPETEER_ * env 变量

puppeteer vs puppeteer-core

API 用法记录

import puppeteer from 'puppeteer-core'
;(async () => {
  try {
    // 打开浏览器
    const browser = await puppeteer.launch({
      // 手动指定浏览器执行文件地址(puppeteer-core 版本必须手动指定)
      executablePath:
        '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
      // 是否以无头模式运行浏览器
      headless: false
    })

    // 打开新的标签页
    const page = await browser.newPage()

    // 在页面加载前修改某个属性
    await page.evaluateOnNewDocument(() => {
      /**
       * 修改 navigator.webdriver 属性,防止反爬虫
       * (如 boss 直聘会检测 puppeteer 一直 debug)
       **/
      Object.defineProperty(navigator, 'webdriver', {
        get: () => undefined
      })
    })

    // 跳转到指定 url
    await page.goto('https://github.com/maomao1996')

    // 获取页面 html
    const html = await page.content()

    // 获取指定 HTMLElement 对象( document.querySelector )
    const nameEl = await page.$('.p-name.vcard-fullname')

    // 获取指定 HTMLElement 的文本
    const nameText = await page.$eval(
      '.p-name.vcard-fullname',
      (el) => el.innerText
    )
    console.log('nameText', nameText)

    // 关闭标签页
    await page.close()

    // 关闭浏览器
    await browser.close()
  } catch (error) {
    console.log('error', error)
  }
})()

相关资料

Puppeteer Github
Puppeteer 中文文档

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant