-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.js
75 lines (63 loc) · 2.58 KB
/
scrape.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
74
75
const puppeteer = require("puppeteer");
const fs = require("fs");
const path = require("path");
async function extractItems(page) {
// Click on all elements with the class ".w8nwRe" to expand them
const expandableElements = await page.$$('.w8nwRe');
for (const element of expandableElements) {
await element.click();
// Wait for animations or content to load (adjust time as necessary)
await new Promise(r => setTimeout(r, 500))
}
// Now extract the reviews
const reviews = await page.evaluate(() => {
return Array.from(document.querySelectorAll(".jftiEf")).map((el) => {
return {
user: {
name: el.querySelector(".d4r55")?.textContent.trim(),
},
rating: el.querySelector(".kvMYJc")?.getAttribute("aria-label").trim(),
review: el.querySelector(".wiI7pd")?.textContent.trim() || "",
};
});
});
return reviews;
}
const scrollPage = async(page, scrollContainer, itemTargetCount) => {
let items = [];
let previousHeight = await page.evaluate(`document.querySelector("${scrollContainer}").scrollHeight`);
while (itemTargetCount > items.length) {
items = await extractItems(page);
await page.evaluate(`document.querySelector("${scrollContainer}").scrollTo(0, document.querySelector("${scrollContainer}").scrollHeight)`);
await page.evaluate(`document.querySelector("${scrollContainer}").scrollHeight > ${previousHeight}`);
await new Promise(r => setTimeout(r, 5000))
}
return items;
}
function getPlaceName(url) {
const matches = url.match(/maps\/place\/([^/]+)\//);
return matches ? matches[1].replace(/\+/g, ' ') : 'output';
}
const scrapePlaceReviews = async (url) => {
try {
browser = await puppeteer.launch({
args: ["--disabled-setuid-sandbox", "--no-sandbox"],
headless: false
});
const [page] = await browser.pages();
await page.goto(url, { waitUntil: "domcontentloaded" , timeout: 60000});
await new Promise(r => setTimeout(r, 3000))
let data = await scrollPage(page,'.DxyBCb',300);
console.log(data);
const csvString = data.map(review =>
`"${review.user.name}","${review.rating}","${review.review.replace(/"/g, '""')}"`
).join('\n');
fs.writeFileSync(path.join(__dirname, `${getPlaceName(url)}.csv`), `"Name","Rating","Review"\n${csvString}`);
await browser.close();
} catch (e) {
console.log(e);
}
};
// Extract URL from the command line arguments
const url = process.argv[2];
scrapePlaceReviews(url);