-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_scraper.js
63 lines (61 loc) · 2.02 KB
/
web_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
const Queue = require('web-scraper-headless/extension/scripts/Queue')
const Sitemap = require('web-scraper-headless/extension/scripts/Sitemap')
const InMemoryStore = require('web-scraper-headless/extension/scripts/InMemoryStore')
const Scraper = require('web-scraper-headless/extension/scripts/Scraper')
const debug = require('debug')('web-scraper-headless:renew')
const JSDOMBrowser = require('web-scraper-headless/extension/scripts/JSDOMBrowser')
const ChromeHeadlessBrowser = require('web-scraper-headless/extension/scripts/ChromeHeadlessBrowser')
/**
*
* @param sitemap
* @param options
* @param options.browser jsdom|headless
* @param options.pageLoadDelay
* @param options.delay
* @return {*}
*/
module.exports = function (sitemap, options) {
return scrape(sitemap, options)
}
function scrape (sitemapInfo, options = {}) {
return new Promise(function (resolve, reject) {
// sitemap is created twice, once in node another in the browser context.
// In node we don't actually need these variables.
const fakeWindow = {}
const fakeDocument = {}
const fake$ = {}
const q = new Queue()
const store = new InMemoryStore()
const sitemap = new Sitemap(sitemapInfo, {$: fake$, document: fakeDocument, window: fakeWindow})
let BrowserConstructor
switch (options.browser) {
case 'jsdom':
BrowserConstructor = JSDOMBrowser
debug('Jsdom browser selected')
break
case 'headless':
BrowserConstructor = ChromeHeadlessBrowser
debug('Chrome headless browser selected')
break
default:
debug('No browser requested so jsdom was selected as default')
BrowserConstructor = JSDOMBrowser
}
const browser = new BrowserConstructor({
pageLoadDelay: options.pageLoadDelay || 2000
})
const s = new Scraper({
queue: q,
sitemap,
browser,
store,
delay: options.delay || 500
}, {})
s.run(function (err) {
if (err) {
console.error(err);
}
resolve(store.data)
})
})
}