-
Notifications
You must be signed in to change notification settings - Fork 1
/
clusterBazaraki.js
291 lines (256 loc) · 9 KB
/
clusterBazaraki.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import * as fs from "fs";
import vanillaPuppeteer from "puppeteer";
import Stealth from "puppeteer-extra-plugin-stealth";
import { Cluster } from "puppeteer-cluster";
import { addExtra } from "puppeteer-extra";
import {
delay,
urlsBazaraki,
urlsBazarakiRents,
getDateString,
} from "./constants.js";
(async () => {
const puppeteer = addExtra(vanillaPuppeteer);
puppeteer.use(Stealth());
const cluster = await Cluster.launch({
puppeteer,
args: ["--no-sandbox", "--disable-setuid-sandbox"],
maxConcurrency: 2,
concurrency: Cluster.CONCURRENCY_PAGE,
monitor: true,
puppeteerOptions: {
headless: true,
defaultViewport: false,
userDataDir: "./tmp",
},
timeout: 86400000, //24h to timeout
});
// handle on error in one of the pages so it does not crash the script
cluster.on("taskerror", (err, data) => {
console.log(`Error crawling ${data}: ${err.message}`);
});
// --scrape=today --scrape=rents
let list = [];
let urlsToScrape = urlsBazaraki;
const date = getDateString();
const scrapeArgument = process.argv
.find((arg) => arg.startsWith("--scrape"))
?.split("=")[1];
let nameStr = "";
switch (scrapeArgument) {
case "today":
nameStr = `bazaraki_daily_${date}.json`;
break;
case "rents":
nameStr = `bazaraki_rents_${date}.json`;
urlsToScrape = urlsBazarakiRents;
break;
case "fullSales":
default:
nameStr = `bazaraki_full_${date}.json`;
break;
}
await cluster.task(async ({ page, data: url }) => {
await page.goto(url, { timeout: 0 });
let isAdFromToday = false;
let isNextBtnExist = true;
while (isNextBtnExist) {
try {
await page.waitForSelector(
".list-simple__output.js-list-simple__output > .announcement-container"
);
} catch (error) {}
const offerContainer = await page.$$(
".list-simple__output.js-list-simple__output > .announcement-container"
);
// capture all prices and descriptions from the unpaid container
for (const offer of offerContainer) {
let price = "";
let textDescription = "";
let category = "";
let adId = "";
let phoneNumber = "";
let adAuthor = "";
let tagObject = {};
try {
price = await page.evaluate(
(el) =>
el.querySelector(
"div.list-announcement-block > div.announcement-block-link.announcement-block__link > div"
).textContent,
offer
);
} catch (e) {}
try {
textDescription = await page.evaluate(
(el) =>
el.querySelector(
"div.list-announcement-block > div.announcement-block-text.announcement-block__text > div > div.announcement-block__date"
).textContent,
offer
);
} catch (error) {}
// return second span of the div
try {
category = await page.evaluate((el) => {
const spanElements = el.querySelectorAll(
".announcement-block__breadcrumbs > span"
);
const secondSpan = spanElements[1];
return secondSpan.textContent;
}, offer);
} catch (error) {}
const clearedPrice = price.replace(/\s+/g, " ").trim();
// replace . with ,
const newPrice = clearedPrice.replace(/\./g, ",");
const clearTextDescription = textDescription
.replace(/\s+/g, " ")
.trim();
await page.evaluate((offer) => {
offer.scrollIntoView();
}, offer);
// find the link in the offer advert and then open newTab using it
const contentLink = await page.evaluate((offer) => {
const anchor = offer.querySelector("a");
const href = anchor.getAttribute("href");
return href;
}, offer);
const page2 = await page.browser().newPage();
try {
await page2.goto(`https://www.bazaraki.com${contentLink}`, {
timeout: 160000,
});
await page2.bringToFront();
try {
adId = await page2.evaluate(() => {
return document.querySelector(".number-announcement > span")
.textContent;
});
} catch (error) {}
// solve the popup afer showing the phone number
try {
const showPhoneNumberBtn = await page2.$(".phone-author__subtext");
await showPhoneNumberBtn.evaluate((b) => b.click());
await delay(1000);
let isModalHidden = null;
try {
isModalHidden = await page2.evaluate(() => {
return document.querySelector("#ui-id-1") !== null;
}, offer);
} catch (e) {}
if (!isModalHidden) {
await delay(1000);
const agreeBtn = await page2.$(
"#ui-id-1 > div > button.terms-dialog__button.js-agree-terms-dialog"
);
agreeBtn && (await agreeBtn.evaluate((b) => b.click()));
// Move the mouse to a location outside of the modal and click to close it
await page2.mouse.click(0, 0);
}
try {
phoneNumber = await page2.evaluate(() => {
return document
.querySelector(".phone-author__subtext > span")
.textContent.trim();
});
} catch (error) {}
try {
adAuthor = await page2.evaluate(() => {
return document
.querySelector(
"div.list-announcement-right > div > div.author-info > div.author-name.js-online-user"
)
.textContent.trim();
});
} catch (error) {}
} catch (error) {
console.log(error);
}
const summaryContainer = await page2.$(".chars-column");
if (summaryContainer) {
const lists = await summaryContainer.$$("li");
for (const list of lists) {
try {
const listText = await page2.evaluate(
(span) => span.textContent,
list
);
const clearedText = listText.replace(/\s+/g, " ").trim();
// create object out of the string ('Area: 95 m²') and push to the list:
const [key, value] = clearedText.split(": ");
tagObject[key] = value;
} catch (error) {}
}
await delay(4000);
}
const newProperty = {
adId,
price: newPrice,
updatedOn: clearTextDescription,
category: category,
phoneNumber,
adAuthor,
...tagObject,
};
// filter the list to check if current adId already exist and if it does, dont push it to avoid duplicates
const existsInList = list.some((obj) => obj.adId === adId);
isAdFromToday = clearTextDescription.includes("Today");
if (
!existsInList &&
(scrapeArgument === "fullSales" || scrapeArgument === "rents")
) {
list.push(newProperty);
const jsonList = JSON.stringify(newProperty) + ",";
fs.appendFileSync(nameStr, jsonList, function (err) {
if (err) throw err;
});
} else if (
!existsInList &&
scrapeArgument === "today" &&
isAdFromToday
) {
// if the script is run with --scrape=today, write to file only today's posts
list.push(newProperty);
const jsonList = JSON.stringify(newProperty) + ",";
fs.appendFileSync(nameStr, jsonList, function (err) {
if (err) throw err;
});
}
await page2.close();
await delay(1000);
} catch {
await page2.close();
}
}
// check if button container exists and click next
try {
await page.waitForSelector(".number-list", { visible: true });
} catch (error) {
console.log(error);
}
const nextButton = await page.$(
".number-list-next.js-page-filter.number-list-line"
);
isNextBtnExist = nextButton !== null;
// detect if the next ad is not from today and stop the search
if (scrapeArgument === "today" && !isAdFromToday) {
isNextBtnExist = false;
}
if (isNextBtnExist) {
await nextButton.evaluate((b) => b.click());
// wait for page to fully load
await page.waitForNavigation({ waitUnitl: "networkidle2" });
await delay(2000);
}
}
});
for (const url of urlsToScrape) {
await cluster.queue(url);
}
await cluster.idle();
await cluster.close();
// adding the array brackets at the end of the script
const fileData = fs.readFileSync(nameStr, "utf8");
const newData = "[" + fileData + "]";
fs.writeFileSync(nameStr, newData, "utf8");
})();