This repository has been archived by the owner on Sep 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
scraper.js
73 lines (58 loc) · 2.01 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
72
73
const puppeteer = require("puppeteer");
const fs = require('fs');
const loadFirebase = require('./lib/db.js');
(async () => {
// Extract partners on the page, recursively check the next page in the URL pattern
const extractPartners = async url => {
// Scrape the data we want
const page = await browser.newPage();
await page.goto(url);
const partnersOnPage = await page.evaluate(() =>
Array.from(document.querySelectorAll("div.compact")).map(compact => ({
title: compact.querySelector("h3.title").innerText.trim(),
logo: compact.querySelector(".logo img").src
}))
);
await page.close();
// Recursively scrape the next page
if (partnersOnPage.length < 1) {
// Terminate if no partners exist
return partnersOnPage
} else {
// Go fetch the next page ?page=X+1
const nextPageNumber = parseInt(url.match(/page=(\d+)$/)[1], 10) + 1;
const nextUrl = `https://marketingplatform.google.com/about/partners/find-a-partner?page=${nextPageNumber}`;
return partnersOnPage.concat(await extractPartners(nextUrl))
}
};
let partners;
// If there's a local JSON, don't fetch anything
const rawdata = fs.readFileSync('partners.json');
const browser = await puppeteer.launch();
if (rawdata) {
partners = JSON.parse(rawdata)
} else {
const firstUrl =
"https://marketingplatform.google.com/about/partners/find-a-partner?page=1";
partners = await extractPartners(firstUrl);
}
// Todo: Update database with partners
console.log(partners);
// loadFirebase().firestore().collection('agencies')
// .limit(10)
// .get()
// .then(snapshot => {
// let data = []
// snapshot.forEach((doc) => {
// data.push({
// id: doc.id,
// ...doc.data()
// })
// })
// return { agencies: data }
// })
// // Save to JSON file
// const data = JSON.stringify(partners);
// fs.writeFileSync('partners.json', data);
await browser.close();
})();