Cover image for Downloading recent Instagram photos using instascrape and Python

Downloading recent Instagram photos using instascrape and Python

chrisgreening profile image Chris Greening ・2 min read

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

πŸ“· 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 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

Super lightweight Python 3 Instagram scraper designed for analytics and content downloading

instascrape logo

instascrape: Instagram scraping for humans

What is it?

instascrape is a powerful, lightweight library for scraping Instagram data without using their API. It is designed with flexibility and developer productivity in mind so you can stop wasting valuable time collecting data and just start analyzing!

Version Language Code style: black Release License

Downloads Activity Dependencies Issues Size

Example showing tech profile scrapes

Key features

  • 🚢 Powerful scraping tools

    • Profile: scrapes 50 data points from a profile
      • follower count
      • recent posts
      • verification status
      • etc.
    • Post: scrapes almost 50 data points from a post
      • likes
      • amount of comments
      • hashtags
      • etc.
    • Hashtag: scrapes over a dozen data points from a hashtag
      • amount of posts
      • recent posts
      • featured picture URL
      • etc.
  • πŸ’ƒ Flexibile: sources for scraping include

    • hashtag/username/shortcode
    • URL
    • HTML
    • JSON
    • BeautifulSoup

    giving you flexibility and easy integration into your scraping projects

  • πŸ’Ύ Download content media locally as png, jpg, mp4, and mp3

  • 🎼 Expressive and consistent API for concise and elegant code

  • πŸ“Š Designed…


Editor guide