DEV Community

loading...
Cover image for Tracking an Instagram posts engagement in real time with instascrape

Tracking an Instagram posts engagement in real time with instascrape

chrisgreening profile image Chris Greening ・2 min read

In this post, I'm going to track the engagement of a single Instagram post in real time using the open source Python Instagram scraping library instascrape 🐍.

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

Version Downloads Release License

Activity Dependencies Issues Code style: black

What is it?

instascrape is a lightweight Python package that provides expressive and flexible tools 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

Here are a few of the things that instascrape does well:

  • Powerful, object-oriented scraping tools for profiles, posts, hashtags, reels, and IGTV
  • Scrapes HTML, BeautifulSoup, and JSON
  • Download content to your computer as png, jpg, mp4, and mp3
  • Dynamically retrieve HTML embed code for posts
  • Expressive and consistent API for concise and elegant code
  • Designed for seamless integration with Selenium, Pandas, and other industry standard tools for data collection and analysis
  • Lightweight; no boilerplate or configurations necessary
  • The only hard dependencies are Requests and Beautiful

For the purposes of this blog post, we'll be scraping a single post across one hour. This can easily be extended however to span more posts over longer periods of time with just a couple modifications.

The script

Since instascrape abstracts out a lot of the tedious work, the script is relatively straightforward:

import time 
import datetime 

import instascrape

def track_post(url: str):
    """
    Return a list of datetimes and a list of strings from an 
    Instagram post scraped across a one hour period.
    """ 
    times = [] 
    likes = []
    now = datetime.datetime.now()
    end_time = now + datetime.timedelta(hours=1)

    while now < end_time:
        time.sleep(60)     

        post = instascrape.Post(url) 
        post.scrape()

        now = datetime.datetime.now()

        times.append(now)
        likes.append(post.likes)
    return times, likes
Enter fullscreen mode Exit fullscreen mode

Now all we have to do is pass a valid Instagram post URL to track_post, wait an hour, and we'll have our data! ⌚

Collecting the data

dates, likes = track_post('https://www.instagram.com/p/CH3E7omnUBj/')
Enter fullscreen mode Exit fullscreen mode

Alt Text

Great! Now plotting our data using matplotlib gives us

Alt Text

In conclusion

Therefore, we're able to track growth of an Instagram post using instascrape very easily 🙌. This was just a simple example but can have many applications. You can compare growth at different times of day, track how your posts perform over time, analyze how posts grow, etc.

If you're interested in learning more about instascrape, check out some of my other blog posts

or better yet, drop the official repository a star ⭐ and get involved with contributions!

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

Version Downloads Release License

Activity Dependencies Issues Code style: black

What is it?

instascrape is a lightweight Python package that provides expressive and flexible tools 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

Here are a few of the things that instascrape does well:

  • Powerful, object-oriented scraping tools for profiles, posts, hashtags, reels, and IGTV
  • Scrapes HTML, BeautifulSoup, and JSON
  • Download content to your computer as png, jpg, mp4, and mp3
  • Dynamically retrieve HTML embed code for posts
  • Expressive and consistent API for concise and elegant code
  • Designed for seamless integration with Selenium, Pandas, and other industry standard tools for data collection and analysis
  • Lightweight; no boilerplate or configurations necessary
  • The only hard dependencies are Requests and Beautiful

Discussion

pic
Editor guide