can u help me with a tip :D. Here is my code:
const request = require('request');
const cheerio = require('cheerio');
function getUrls(url) {
const baseUrl = 'https://unsplash.com';
let urls = [];
request(url, (err, res, body) => {
if (!err && res.statusCode === 200) {
const $ = cheerio.load(body, { normalizeWhitespace: false, xmlMode: false, decodeEntities: true });
$('.photo.qa-photo a').each((i, e) => {
const lnk = $(e).attr('href');
if (lnk.indexOf('@') === -1 && lnk.indexOf('download') === -1) {
urls.push(baseUrl + lnk);
}
});
}
});
return urls;
}
function getImages(arr) {
let images = [];
for (const url of arr) {
request(url, (err, res, body) => {
if (!err && res.statusCode === 200) {
const $ = cheerio.load(body, { normalizeWhitespace: false, xmlMode: false, decodeEntities: true });
$('script').each((i, e) => {
if (i === 4) {
let img = $(e).text();
img = img.substring(img.indexOf('full') + 7, img.indexOf('regular') - 3);
images.push(img);
}
});
} else {
console.log(err, res.statusCode);
}
});
}
return images;
}
console.log(getImages(getUrls('https://unsplash.com/search?utf8=%E2%9C%93&keyword=life&button=')));
Separately functions are working well, but if i want to combine both functions console.log show me an empty array first then functions finish work but i cant see the returned array, why? sorry for my english. With this little programm i want to get full urls of 20 images from unsplash.com Ty in advance.