So I want to use request-promise to pull the body of a page. Once I have the page I want to collect all the tags and get an array of src's of those images. Assume the src attributes on a page have both relative and absolute paths. I want an array of absolute paths for imgs on a page. I know I can use some string manipulation and the npm path to build the absolute path but I wanted to find a better way of doing it.
var rp = require('request-promise'),
cheerio = require('cheerio');
var options = {
uri: 'http://www.google.com',
method: 'GET',
resolveWithFullResponse: true
};
rp(options)
.then (function (response) {
$ = cheerio.load(response.body);
var relativeLinks = $("img");
relativeLinks.each( function() {
var link = $(this).attr('src');
console.log(link);
if (link.startsWith('http')){
console.log('abs');
}
else {
console.log('rel');
}
});
});
results
/logos/doodles/2016/phoebe-snetsingers-85th-birthday-5179281716019200-hp.gif
rel