So I found a website that has very cool images and I'd like to scrape some of its data. The website didn't get any update for about 5 years and I tried to contact its owner for some kind of API and I didn't get any response back.
Anyway, the website has categories and each image has it's own page number; so in order to scrape every image, I need to go to each category and then to go to each page of that particular category.
Below is my code, but I can't make the for loop
to reset.
const {Cluster} = require('puppeteer-cluster');
const puppeteer = require('puppeteer');
let c = 0;
let z = 500;
(async () => {
process.setMaxListeners(5);
const cluster = await Cluster.launch({
maxConcurrency: 3 // max browsers to spawn at the same time
});
let b = 20;
for (let i = 0; i < b; i++) {
cluster.execute({i}, async () => {
let browser = await puppeteer.launch({headless: false});
// scraping code using the i and c values
await browser.close();
console.log(i);
if (i > b - 10) {
i = 0;
c = c + 1;
console.log('c = ' + c);
if (c > z)
process.exit();
}
});
}
await cluster.idle();
await cluster.close();
})();
This is the output (the order isn't necessary):
1
0
2
4
3
5
6
7
8
9
10
11
c = 1
12
c = 2
13
c = 3
14
c = 4
16
c = 5
15
c = 6
17
c = 7
18
c = 8
19
c = 9
Process finished with exit code 0
If I add await
in front of cluster.execute
then the for loop
is resetting, but then I can't use multiple browsers at the same time.
Edit:
const {Cluster} = require('puppeteer-cluster');
const puppeteer = require('puppeteer');
(async () => {
process.setMaxListeners(5);
const cluster = await Cluster.launch({maxConcurrency: 3});
let b = 15;
let d;
function myLoop() {
let g = 0;
for (g; g <= n; g++) {
console.log(g);
myFunc();
}
return g;
}
d = myLoop();
console.log('d: ' + d);
if (d > 0)
myLoop();
async function myFunc() {
await cluster.execute(async () => {
let browser = await puppeteer.launch({headless: false});
await browser.close();
});
}
await cluster.idle();
await cluster.close();
})();