-
Notifications
You must be signed in to change notification settings - Fork 98
/
scrapeLogic.js
51 lines (43 loc) · 1.44 KB
/
scrapeLogic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const puppeteer = require("puppeteer");
require("dotenv").config();
const scrapeLogic = async (res) => {
const browser = await puppeteer.launch({
args: [
"--disable-setuid-sandbox",
"--no-sandbox",
"--single-process",
"--no-zygote",
],
executablePath:
process.env.NODE_ENV === "production"
? process.env.PUPPETEER_EXECUTABLE_PATH
: puppeteer.executablePath(),
});
try {
const page = await browser.newPage();
await page.goto("https://developer.chrome.com/");
// Set screen size
await page.setViewport({ width: 1080, height: 1024 });
// Type into search box
await page.type(".search-box__input", "automate beyond recorder");
// Wait and click on first result
const searchResultSelector = ".search-box__link";
await page.waitForSelector(searchResultSelector);
await page.click(searchResultSelector);
// Locate the full title with a unique string
const textSelector = await page.waitForSelector(
"text/Customize and automate"
);
const fullTitle = await textSelector.evaluate((el) => el.textContent);
// Print the full title
const logStatement = `The title of this blog post is ${fullTitle}`;
console.log(logStatement);
res.send(logStatement);
} catch (e) {
console.error(e);
res.send(`Something went wrong while running Puppeteer: ${e}`);
} finally {
await browser.close();
}
};
module.exports = { scrapeLogic };