Skip to content

Instantly share code, notes, and snippets.

@spinningcat
Created October 31, 2023 10:08
Show Gist options
  • Save spinningcat/15297467cdf03be10501908ff48916f3 to your computer and use it in GitHub Desktop.
Save spinningcat/15297467cdf03be10501908ff48916f3 to your computer and use it in GitHub Desktop.
const puppeteer = require("puppeteer");
(async () => {
const results = []; // Declare 'results' array in the wider scope
const browser = await puppeteer.launch({
headless: false,
args: ["--disable-features=EnableUserAgentClientHint"],
});
const page = await browser.newPage();
try {
await page.goto(
"https://www.apkmirror.com/uploads/?appcategory=instagram-instagram",
{ waitUntil: "domcontentloaded" }
);
while (true) {
const data = await page.evaluate(() => {
const elements = document.querySelectorAll(".appRow");
elements.forEach((element) => {
const titleElement = element.querySelector(
".table-cell:nth-child(2) div h5"
);
if (titleElement) {
const title = titleElement.getAttribute("title");
if (
title &&
title.includes("Instagram") &&
!title.includes("beta") &&
!title.includes("alpha")
) {
results.push(title);
}
}
});
});
if (await page.$(".page.larger")) {
// If there is a "Next" button, click it to go to the next page
await page.click(".page.larger");
// Wait for the .appRow elements to appear on the second page
await page.waitForSelector(".appRow", { visible: true });
} else {
// No more "Next" button found, exit the loop
if (results.length >= 10) {
// Change this to '>='
break;
}
}
}
} catch (error) {
console.error("An error occurred:", error);
} finally {
// await browser.close();
console.log(results);
}
})();
An error occurred: Error [ReferenceError]: results is not defined
pptr:evaluate;%2Fvar%2Fwww%2Fhtml%2Finstagramapaiaa%2Findex.js%3A19%3A31:18:15
at evaluate (evaluate at /var/www/html/instagramapaiaa/index.js:19:31, <anonymous>:17:14)
at evaluate (evaluate at /var/www/html/instagramapaiaa/index.js:19:31, <anonymous>:3:17)
at #evaluate (/var/www/html/instagramapaiaa/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/ExecutionContext.js:229:55)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async ExecutionContext.evaluate (/var/www/html/instagramapaiaa/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/ExecutionContext.js:126:16)
at async IsolatedWorld.evaluate (/var/www/html/instagramapaiaa/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/IsolatedWorld.js:128:16)
at async CdpFrame.evaluate (/var/www/html/instagramapaiaa/node_modules/puppeteer-core/lib/cjs/puppeteer/api/Frame.js:370:20)
at async CdpPage.evaluate (/var/www/html/instagramapaiaa/node_modules/puppeteer-core/lib/cjs/puppeteer/api/Page.js:900:20)
at async /var/www/html/instagramapaiaa/index.js:19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment