Skip to content

Instantly share code, notes, and snippets.

@dmdboi
Created April 18, 2020 23:52
Show Gist options
  • Save dmdboi/b182cd54380e9b90891925b840be8649 to your computer and use it in GitHub Desktop.
Save dmdboi/b182cd54380e9b90891925b840be8649 to your computer and use it in GitHub Desktop.
This code snippet downloads a single image from an Instagram URL using Puppeteer and Cheerio
const download = require('image-downloader')
const puppeteer = require('puppeteer')
const $ = require('cheerio');
const saveImage = (urlVar, callback) => {
puppeteer
.launch()
.then(function(browser) {
return browser.newPage();
})
.then(function(page) {
return page.goto(urlVar).then(function() { //Replace urlVar with variable of the IG post you want to download
return page.content();
});
})
.then(function(html) {
var images = $('img', html)[1].attribs.srcset //There are multiple img tags on a page, [1] is the image you want.
var list = images.split(',') //The img tag has a source set so this parses it and finds a link that works
var temp = list[2]
var downloadImage = temp.split(" ")
const options = {
url: downloadImage[0],
dest: './downloads/'
}
download.image(options)
.then(({ filename, image }) => {
return callback(console.log('Saved to', filename))
})
.catch(err => {
console.log(err);
})
})
.catch(function(err) {
console.log(err)
});
}
exports.saveImage = saveImage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment