DEV Community

Cover image for Downloading recent Instagram photos using instascrape and Python
Chris Greening
Chris Greening

Posted on • Updated on

Downloading recent Instagram photos using instascrape and Python

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('chris_greening')
chris.scrape()
Enter fullscreen mode Exit fullscreen mode

⌚ 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]
Enter fullscreen mode Exit fullscreen mode

💻 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")
Enter fullscreen mode Exit fullscreen mode

📷 The result

Alt Text

It's as easy as that!

If you want to learn more about instascrape, check out some of my other posts

or better yet, come on over to the official repo and leave it a star! I'm always looking for new contributors 😄.

GitHub logo chris-greening / instascrape

Powerful and flexible Instagram scraping library for Python, providing easy-to-use and expressive tools for accessing data programmatically

instascrape: powerful Instagram data scraping toolkit

Note: This module is no longer actively maintained.

DISCLAIMER:

Instagram has gotten increasingly strict with scraping and using this library can result in getting flagged for botting AND POSSIBLE DISABLING OF YOUR INSTAGRAM ACCOUNT. This is a research project and I am not responsible for how you use it. Independently, the library is designed to be responsible and respectful and it is up to you to decide what you do with it. I don't claim any responsibility if your Instagram account is affected by how you use this library.

Version Downloads Release License

Activity Dependencies Issues

What is it?

instascrape is a lightweight Python package that provides an expressive and flexible API for scraping Instagram data. It is geared towards being a high-level building block on the data scientist's toolchain and can be seamlessly integrated and extended with industry standard tools for web scraping, data science, and analysis.

Key features

…

Top comments (6)

Collapse
 
adiprakoso profile image
adi-prakoso

Is there any way to change setting of get_recent_posts() from 12 posts to, for example, 50 or 100 posts?

Collapse
 
villival profile image
villival

tried to run the script but could not find any downloads :(

any glitch in the code

Collapse
 
chrisgreening profile image
Chris Greening

Hello! Could you post the traceback for the error? I'm not able to reproduce the same problem on my machine, it's possible that Instagram is redirecting you to the login page due to not being logged in/too many requests

Collapse
 
apet profile image
apet

I was working on a script and the download method threw the error:
AttributeError: 'Post' object has no attribute 'is_video'

Do you know what might cause this?

Collapse
 
yaduvarman profile image
yaduvarman • Edited

can we download just single post from an user,like you enter the URL of a post and then it just downloads.
....can you please tell me

Collapse
 
chrisgreening profile image
Chris Greening

Yes! Just use the Post object on it's own like so:

from instascrape import Post
google_post = Post("https://www.instagram.com/p/CGiWeQjl4DI/")
google_post.download("/your/image/path.png")
Enter fullscreen mode Exit fullscreen mode