-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.js
71 lines (52 loc) · 2.24 KB
/
scraper.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { TimeoutError } from "puppeteer";
import puppeteer from "puppeteer-extra";
let scrapeKeywords = async (searchKeyword) => {
const KeywordsEverywhereDir = "./keywordsEverywhere"
const browser = await puppeteer.launch({
headless: false,
args: [
`--disable-extensions-except=${KeywordsEverywhereDir}`,
`--load-extension=${KeywordsEverywhereDir}`,
],
});
// create browser context
const context = await browser.defaultBrowserContext();
context.overridePermissions('https://google.com', ['clipboard-read']);
const page = await browser.newPage()
// refresh page
await page.setViewport({ width: 1280, height: 1080, deviceScaleFactor: 1 })
await page.reload()
await page.goto("https://google.com", { waitUntil: 'load', timeout: 35000 });
page.bringToFront()
// locale search bar
const searchbox = await page.waitForSelector(".SDkEP")
await searchbox.type(searchKeyword)
await searchbox.press("Enter")
// wait for selector of Keywords Everywhere dom elements
let relatedSearchSelector = ("#xt-related-search .xt-g-table-no-credits")
let longTailKeywordsSelector = "#xt-google-ltkwid .xt-g-table-no-credits"
let relatedSearchTableContents;
let longtTailKeywordsContents;
await page.waitForNavigation()
try {
if (await page.$(relatedSearchSelector) !== null) {
relatedSearchTableContents = await page.$eval(relatedSearchSelector, (el) => { return el.getInnerHTML() });
} else {
relatedSearchTableContents = "Table not present"
}
if (await page.$(longTailKeywordsSelector) !== null) {
longtTailKeywordsContents = await page.$eval(longTailKeywordsSelector, (el) => { return el.getInnerHTML() });
} else {
longtTailKeywordsContents = "Table not present"
// console.log("from scraper",longtTailKeywordsContents)
}
// console.log("from scraper",relatedSearchTableContents)
return [relatedSearchTableContents, longtTailKeywordsContents]
} catch (err) {
console.log("Some Error occured: ", err)
return "Table data missing"
} finally {
await browser.close()
}
}
export { scrapeKeywords }