正如标题所示,我只想访问在 getSubURLs() 函数中的 getURLs() 中填充的全局数组数据,以便我可以操作其中的数据。有人可以让我知道我该怎么做吗?提前非常感谢!!
const cheerio = require('cheerio');
const axios = require('axios');
let URL = 'https://toscrape.com';
const urlQueue = [];
const getURLS = async () => {
await axios
.get(URL)
.then((res) => {
const data = res.data;
const $ = cheerio.load(data);
$("a[href^='http']").each((i, elem) => {
const link = $(elem).attr('href');
if (urlQueue.indexOf(link) === -1) {
urlQueue.push(link);
}
});
console.log(urlQueue);
return urlQueue;
})
.catch((err) => {
console.log(`Error fetching and parsing data: `, err);
});
};
const getSubURLs = async () => {
// call urlqueue array here after it finishes being created
}
getURLS();
一只名叫tom的猫
相关分类