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!
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()
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]
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")
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 😄.