1 回答

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個(gè)贊
不要async與.then語法混合,只與await結(jié)果混合:
const cheerio = require('cheerio');
const axios = require('axios');
let URL = 'https://toscrape.com';
const urlQueue = [];
const getURLS = async () => {
try {
const res = await axios.get(URL);
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);
} catch (err) {
console.log(`Error fetching and parsing data: `, err);
}
};
// in case there's no top-level await
(async () => {
await getURLS();
// do something about urlQueue
})();
添加回答
舉報(bào)