javascript Puppeteer Snippets

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript Puppeteer Snippets相关的知识,希望对你有一定的参考价值。

const puppeteer = require('puppeteer')
const express = require('express')
const app = express()
const CREDS = require('./creds')

console.log(`${new Date()} > Started`)

async function genPDF(url) {
// Init
  const browser = await puppeteer.launch({executablePath: 'C:\\Users\\43844836\\programs\\chrome-win32\\chrome.exe', headless: true});
  const page = await browser.newPage()
  await page.goto('https://www.instapaper.com/user/login')

// Logging start
  const USERNAME_SELECTOR = '#username'
  const PASSWORD_SELECTOR = '#password'
  const BUTTON_SELECTOR = '#log_in'
  await page.click(USERNAME_SELECTOR)
  await page.type(CREDS.username_instapaper)
  await page.click(PASSWORD_SELECTOR)
  await page.type(CREDS.password_instapaper)
  await page.click(BUTTON_SELECTOR)
  console.log(`${new Date()} > Logging in`)
  await page.waitForNavigation()
  console.log(`${new Date()} > Landed at home, Navigating to article page`)

// Goto article page
  await page.goto(url)
  console.log(`${new Date()} > Landed at article page, generating PDF`)
  const articleTitleRaw = await page.$eval('main div#titlebar h1', el => el.innerHTML)
  const articleTitle = articleTitleRaw.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '')

// Generate PDF
  await page.pdf({path: `pdf/${articleTitle}.pdf`, format: 'A5'})
  console.log(`${new Date()} > Export PDF completed as name ${articleTitle}.pdf at pdf directory`)
  browser.close()
}

app.post('/genpdf', function(req, res) {
  
  let instaArticleUrl = `https://www.instapaper.com/text?u=${req.query.url}`

  if (req.query.token == 'mytoken') {
    console.log('genpdf function called')
    genPDF(instaArticleUrl)
    res.send('Node Version: ' + process.version)
  } else {
    console.log('invilid token')
    res.send('Node Version: ' + process.version)
  }

})

app.listen(1337)
const puppeteer = require('puppeteer');
// let box;
(async () => {
  const browser = await puppeteer.launch({headless: true});
  const page = await browser.newPage();
  let q = 'define toboggan'
  await page.setViewport({width: 2560, height: 1440});  
  await page.goto(`https://www.google.com/search?q=${q}&pws=0&gl=us&gws_rd=cr`);
  const first_card = await page.$('div#center_col div._NId');
  let box = first_card.boundingBox()
  let boxSize = await box.then(r => r)
  await page.screenshot({
    path: `./images/${q}.png`, 
    clip: {
      x: boxSize.x - 15, 
      y: boxSize.y, 
      width: boxSize.width + 30, 
      height: boxSize.height
    }
  });
  await browser.close();
})();

以上是关于javascript Puppeteer Snippets的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Puppeteer 和纯 JavaScript 检查元素是不是可见?

Javascript/HTML/Puppeteer - 如何访问属性数据绑定中的值(单击按钮)?

javascript 使用chrome和puppeteer拦截响应

javascript 从puppeteer导出所有用户/会话数据

javascript Puppeteer Snippets

javascript puppeteer1.js