-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathGoogleMapsPlacesScraper.js
More file actions
132 lines (125 loc) · 5.53 KB
/
GoogleMapsPlacesScraper.js
File metadata and controls
132 lines (125 loc) · 5.53 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const puppeteer = require("puppeteer");
const extractData = async (page) => {
let items = await page.evaluate(() => {
let i = 0;
return {
title: document.querySelector(".fontHeadlineLarge")?.textContent,
rating: document.querySelector(".F7nice")?.textContent,
reviews: document.querySelector(".mmu3tf .DkEaL")?.textContent,
type: document.querySelector(".u6ijk")?.textContent,
service_options: document.querySelector(".E0DTEd")?.textContent.replaceAll("·", ""),
address: document.querySelector("button[data-tooltip='Copy address']")?.textContent.trim(),
website: document.querySelector("a[data-tooltip='Open website']")?.textContent.trim(),
pluscode: document.querySelector("button[data-tooltip='Copy plus code']")?.textContent.trim(),
timings: Array.from(document.querySelectorAll(".OqCZI tr")).map((el) => {
return {
[el.querySelector("td:first-child")?.textContent.trim()]: el.querySelector("td:nth-child(2) li.G8aQO")?.textContent,
};
}),
popularTimes: {
graph_data: Array.from(document.querySelectorAll(".C7xf8b > div")).map((el) => {
let day;
if (i == 0) {
day = "Sunday"
}
else if (i == 1) {
day = "Monday"
}
else if (i == 2) {
day = "Tuesday"
}
else if (i == 3) {
day = "Wednesday"
}
else if (i == 4) {
day = "Thursday"
}
else if (i == 5) {
day = "Friday"
}
else if (i == 6) {
day = "Saturday"
}
i++;
return {
[day]: Array.from(el.querySelectorAll(`.dpoVLd`)).map((el) => {
const time = el.getAttribute("aria-label").split("at")[1].trim();
const busy_percentage = el.getAttribute("aria-label").split("busy")[0].trim();
return {
time,
busy_percentage,
};
}),
};
}),
},
photos: Array.from(document.querySelectorAll(".dryRY .ofKBgf")).map((el) => {
return {
title: el.getAttribute("aria-label"),
thumbnail: el.querySelector("img").getAttribute("src"),
}
}),
question_and_answers: {
question: document.querySelector(".Py6Qke")?.textContent,
answer: document.querySelector(".l79Qmc").textContent
},
user_ratings: Array.from(document.querySelectorAll(".ExlQHd tr")).map((el) => {
return {
[el.getAttribute("aria-label")?.split(",")[0].trim()]: el.getAttribute("aria-label")?.split(",")[1].trim(),
};
}),
user_reviews: Array.from(document.querySelectorAll(".tBizfc")).map((el) => {
return {
description: el.textContent.replaceAll('"', "").trim(),
user_link: el.querySelector("a").getAttribute("href")
}
}),
mentions: Array.from(document.querySelectorAll(".KNfEk+ div .L6Bbsd")).map((el) => {
return {
query: el.querySelector(".uEubGf").textContent,
mentioned: el.querySelector(".fontBodySmall").textContent + "times"
}
}),
most_relevant: Array.from(document.querySelectorAll(".jJc9Ad")).map((el) => {
return {
user: {
name: el.querySelector(".d4r55")?.textContent,
thumbnail: el.querySelector(".NBa7we")?.getAttribute("src"),
local_guide: el.querySelector(".RfnDt span:nth-child(1)")?.textContent.length ? true : false,
reviews: el.querySelector(".RfnDt span:nth-child(2)")?.textContent.replace(".", "").trim(),
link: el.querySelector(".WEBjve")?.getAttribute("href")
},
rating: el.querySelector(".kvMYJc")?.getAttribute("aria-label"),
date: el.querySelector(".rsqaWe")?.textContent,
review: el.querySelector(".MyEned .wiI7pd").textContent,
images: Array.from(el.querySelectorAll(".KtCyie button")).length ? Array.from(el.querySelectorAll(".KtCyie button")).map((el) => {
return {
thumbnail: getComputedStyle(el).backgroundImage.split('")')[0].replace('url("', ""),
};
})
: "",
}
})
}
});
return items;
}
const getMapsPlacesData = async () => {
try {
const url = "https://www.google.com/maps/place/Blacklist+Coffee+Roasters/@-31.9473,115.8073705,14z/data=!4m13!1m7!3m6!1s0x0:0xf79bec80595c6aa8!2sBlacklist+Coffee+Roasters!8m2!3d-31.9472988!4d115.8248801!10e1!3m4!1s0x0:0xf79bec80595c6aa8!8m2!3d-31.9472988!4d115.8248801";
browser = await puppeteer.launch({
headless: false,
args: ["--disabled-setuid-sandbox", "--no-sandbox"],
});
const [page] = await browser.pages();
await page.goto(url, { waitUntil: "domcontentloaded", timeout: 60000 });
await page.waitForTimeout(3000);
const data = await extractData(page);
console.log(data)
await browser.close();
}
catch (e) {
console.log(e);
}
}
getMapsPlacesData();