Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chris-greening/c7dbe700a16dc87abf86f164c9c45333 to your computer and use it in GitHub Desktop.
Save chris-greening/c7dbe700a16dc87abf86f164c9c45333 to your computer and use it in GitHub Desktop.

In this post, I'm going to show you how to download a user's recent Instagram photos programatically using the expressive and lightweight instascrape library in 3 easy steps!

👀 Loading the profile

To start, we will use instascrape.Profile to load an Instagram profile's necessary data (for this example, we'll use my page @chris_greening.

from instascrape import Profile 
chris = Profile.from_username('chris_greening')
chris.load()

⌚ Getting the recent posts

From this Profile object, we can now get a list of instascrape.Post objects and filter them so that we don't download any videos

recents = chris.get_recent_posts()
chris_photos = [post for post in recents if not post.is_video]

💻 Downloading the images

And now the moment we've all been waiting for! instascrape.Post provides the download method which takes a filepath string as an argument to download our image to.

We're going to loop through all of the Post instances in chris_photos and create a unique filename based on its datetime stored in upload_date (i.e. "2020-09-09 10h24m.png").

for post in chris_photos: 
    fname = post.upload_date.strftime("%Y-%m-%d %Hh%Mm")
    post.download(f"{fname}.png")

📷 The result

Alt Text

It's as easy as that! Currently, instascrape only supports downloading images but I hope to expand it in the near future to videos as well 🎥

If you want to learn more, come on over to the official repo and leave it a star! I'm always looking for new contributors 😄.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment