DEV Community

Cover image for Tracking an Instagram posts engagement in real time with instascrape
Chris Greening
Chris Greening

Posted on

Tracking an Instagram posts engagement in real time with instascrape

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

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

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

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 (1)

Collapse
 
majidja516 profile image
majidja516

I am really gratitude for writing such a useful and concisely Instagram package . Because I mainly focus on R, your examples are very helpful.
I have a question about extracting comments from a post.
I will be wondering, If you provide an example in this issue that shows how I can get all comments in a post and its related parameters.
Thanks a lot