Tengo una pestaña que quiero usar en una función.
const extractLinks = async (url, arrLinks) => { // do things on arrLinks return arrLinks; }Quiero crear una pestaña al comienzo de mi programa y llamar varias veces a la función extractLinks en la misma pestaña, para tener una pestaña con muchos valores:
let arrLinks = []; // New tab for (let cptpages = 1; cptpages < 33; cptpages++) { const URL = 'https://droidsoft.fr/category/tests-android/page/' + cptpages; extractLinks(URL, arrLinks); }Cuando hago eso, si pongo un archivo console.log(arrLinks), en la función, imprime el valor, pero después de la iteración, la pestaña está vacía.
Usted me podría ayudar ?
Todo mi código:
// Kindacode.com const cheerio = require('cheerio'); const got = (...args) => import('got').then(({ default: got }) => got(...args)); // You cannot use "require" with the latest version of got // If you're using ES Module or TypeScript, just import got like this: import got from 'got' const extractLinks = async (url, arrLinks) => { try { // Fetching HTML const response = await got(url); const html = response.body; // Using cheerio to extract <a> tags const $ = cheerio.load(html); const linkObjects = $('a'); // this is a mass object, not an array // Collect the "href" and "title" of each link and add them to an array const links = []; linkObjects.each((index, element) => { if ($(element).attr('href').startsWith('https://droidsoft.fr/202') && $(element).attr('href').includes('test-')) { links.push($(element).attr('href')); } }); arrLinks = [...new Set(links)]; console.log(arrLinks); return arrLinks; // do something else here with these links, such as writing to a file or saving them to your database } catch (error) { console.log(error); } }; // Try it let arrLinks = []; // Tableau vide à la base for (let cptpages = 1; cptpages < 33; cptpages++) { // On parcourt les 33 pages de tests-android const URL = 'https://droidsoft.fr/category/tests-android/page/' + cptpages; extractLinks(URL, arrLinks); // On appelle la fonction pour récuperer les URL de chaque test, de chaque page, avec le tableau initial } console.log(arrLinks);Parece que estás reasignando arrLinks cada vez, como se ve en la parte inferior del fragmento de código como: arrLinks = [...new Set(links)];
En su lugar, querrá hacer uno de los siguientes:
arrLinks.push([...new Set(links)])arrLinks = [...arrLinks, ...new Set(links)]Alternativamente, podrías tomar una ruta completamente diferente:
const extractLinks = async (url) => { try { // Fetching HTML const response = await got(url); const html = response.body; // Using cheerio to extract <a> tags const $ = cheerio.load(html); const linkObjects = $('a'); // this is a mass object, not an array // Collect the "href" and "title" of each link and add them to an array const links = []; linkObjects.each((index, element) => { if ($(element).attr('href').startsWith('https://droidsoft.fr/202') && $(element).attr('href').includes('test-')) { links.push($(element).attr('href')); } }); return links // return the array of links itself } catch (error) { console.log(error); } }; // Try it let arrLinks = []; // Tableau vide à la base for (let cptpages = 1; cptpages < 33; cptpages++) { // On parcourt les 33 pages de tests-android const URL = 'https://droidsoft.fr/category/tests-android/page/' + cptpages; // now we're keeping the array each time and // simply adding the results of the function to it arrLinks.push(await extractLinks(URL)) // may need to wrap loop in async func to use await } // If you want arrLinks to be a completely flat array // (since it will will be an array of arrays, you can just // call the `.flat()` method after you're finished like so `arrLinks.flat()` console.log(arrLinks);