我编写了一个程序来提取链接以通过三个步骤下载照片:
getPersons() 函数获取要遍历的人员的完整列表。
从人员列表中获取照片下载链接。
从步骤 2 中创建的列表下载。
我正在尝试将第 2 步重构为异步函数。是否有一种简单的方法可以将第二步重构为函数以使代码更具可读性?
理想情况下,我想检索所有链接,然后才开始下载。
const axios = require("axios");
const cheerio = require("cheerio");
const url = "https://www.website.com";
const persons = [];
async function getPersons() {
await axios.get(url).then(response => {
const html = response.data;
const $ = cheerio.load(html);
const personList = $(".bio-btn");
console.log(personList.length);
personList.each(function() {
const link_raw = $(this).attr("href");
const link = url + link_raw;
const name = link_raw.replace("/bio/", "");
person.push({
name,
link
});
});
});
}
getPersons().then(function() {
persons.forEach(async function(person) {
var personLink = person.link;
await axios.get(personLink).then(response => {
const html = response.data;
const $ = cheerio.load(html);
const snapshots = $(".ratio-4-3");
snapshots.each(function() {
const pic = $(this).attr("style");
if (pic != undefined && pic.includes("biopicture")) {
var bioPhoto = s[1];
}
});
});
});
});
POPMUISE
慕的地10843
富国沪深
随时随地看视频慕课网APP
相关分类