Skip to content

Instantly share code, notes, and snippets.

@spinningcat
Created October 31, 2023 11:25
Show Gist options
  • Save spinningcat/5b0af5b674d36ab2389e834acc34bbea to your computer and use it in GitHub Desktop.
Save spinningcat/5b0af5b674d36ab2389e834acc34bbea to your computer and use it in GitHub Desktop.
async.js
const puppeteer = require("puppeteer");
const grabfunc = async (finalArr) => {
//let finalArr = [];
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 results = [];
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")
) {
// console.log(element);
results.push(title);
}
}
});
return results;
});
finalArr = finalArr.concat(data); // Concatenate the data to finalArr
// console.log("finalArr");
// console.log(finalArr);
// console.log(finalArr.length);
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 });
if (finalArr.length >= 9) {
// Change this to '>='
break;
}
} else {
// No more "Next" button found, exit the loop
}
}
} catch (error) {
console.error("An error occurred:", error);
} finally {
//await browser.close();
const uniqueArray = [...new Set(finalArr)]; // Remove duplicates
const newArray = uniqueArray.slice(0, 10); // Get the first 10 unique elements
console.log(newArray); // This will contain the first 10 unique elements from the original array
return newArray;
}
};
const firstResult = async (arr) => {
console.log("arr");
grabfunc(arr);
};
firstResult([]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment