Skip to content

Instantly share code, notes, and snippets.

@korECM
Last active February 13, 2020 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save korECM/b4b5fd6f75f8489f9f87fda4f4b535d4 to your computer and use it in GitHub Desktop.
Save korECM/b4b5fd6f75f8489f9f87fda4f4b535d4 to your computer and use it in GitHub Desktop.
AWS Lambda 크롤링
const chromium = require('chrome-aws-lambda');
module.exports.handler = async (event, context) => {
const browser = await chromium.puppeteer.launch({
executablePath: await chromium.executablePath,
args: chromium.args,
defaultViewport: chromium.defaultViewport,
headless: chromium.headless,
});
const page = await browser.newPage();
let targetUrl = "https://velog.io/@";
try {
let id = event.pathParameters.id;
targetUrl = targetUrl + id;
} catch (e) {
targetUrl = targetUrl + "velopert";
}
await page.goto(targetUrl);
await page.waitForSelector("h2");
const message = await page.$$eval("h2", data => {
return data.map(element => element.textContent);
});
await browser.close();
return {
statusCode: 200,
body: JSON.stringify({
message: ` Data is Here : ${message}`
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment