1 回答
TA贡献1906条经验 获得超3个赞
不要async与.then语法混合,只与await结果混合:
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
})();
添加回答
举报