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()
⌚ 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
It's as easy as that!
If you want to learn more about instascrape
, check out some of my other posts
Visualizing Instagram engagement with instascrape and Python
Chris Greening ・ Oct 21 '20
Creating a scatter matrix of Instagram data using Python
Chris Greening ・ Oct 25 '20
or better yet, come on over to the official repo and leave it a star! I'm always looking for new contributors 😄.
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.
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.
Top comments (6)
Is there any way to change setting of get_recent_posts() from 12 posts to, for example, 50 or 100 posts?
tried to run the script but could not find any downloads :(
any glitch in the code
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
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?
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
Yes! Just use the
Post
object on it's own like so: