Skip to content

Instantly share code, notes, and snippets.

@dorogoff
Forked from Alezco/index.js
Created August 31, 2022 20:29
Show Gist options
  • Save dorogoff/8c618944875e09fcd5037d3c0148fe93 to your computer and use it in GitHub Desktop.
Save dorogoff/8c618944875e09fcd5037d3c0148fe93 to your computer and use it in GitHub Desktop.
Puppeteer in Google Cloud Functions
const puppeteer = require('puppeteer');
const PUPPETEER_OPTIONS = {
headless: true,
args: [
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-setuid-sandbox',
'--timeout=30000',
'--no-first-run',
'--no-sandbox',
'--no-zygote',
'--single-process',
"--proxy-server='direct://'",
'--proxy-bypass-list=*',
'--deterministic-fetch',
],
};
const openConnection = async () => {
const browser = await puppeteer.launch(PUPPETEER_OPTIONS);
const page = await browser.newPage();
await page.setUserAgent(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
);
await page.setViewport({ width: 1680, height: 1050 });
return { browser, page };
};
const closeConnection = async (page, browser) => {
page && (await page.close());
browser && (await browser.close());
};
exports.scrapingExample = async (req, res) => {
let { browser, page } = await openConnection();
try {
await page.goto('https://medium.com/', { waitUntil: 'load' });
const firstArticleTitle = await page.evaluate(() =>
document.querySelector('.extremeHero-titleClamp').innerText
);
res.status(200).send(firstArticleTitle);
} catch (err) {
res.status(500).send(err.message);
} finally {
await closeConnection(page, browser);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment