Visualizing Instagram engagement with instascrape and Python
Tags: python, datascience, opensource, hacktoberfest
Published: 2020-10-21 21:37:45

In a recent post, I introduced my open source Instagram web scraper [`instascrape`](https://github.com/chris-greening/instascrape) as a lightweight means of collecting data from Instagram using Python! 

{% link https://dev.to/chrisgreening/scrape-data-from-instagram-with-instascrape-5e3e %} 

For this post, I'm going to walkthrough an example using one of `instascrape`'s recent additions: the ability to scrape an Instagram user's recent posts! With this data, we'll be able to visualize the trend in engagement for that user and see if their page is growing or declining :raised_hands:.

{% embed https://www.christophergreening.com %}

---

# Scraping the data 
We'll be visualizing data from my Instagram page [@chris_greening](https://www.instagram.com/chris_greening/) (shameless self promo :wink:) but feel free to remove my username and replace it with your own :grimacing:

_Quick note_: If you plan on following along, check out my [recent post](https://dev.to/chrisgreening/scrape-data-from-instagram-with-instascrape-5e3e) for details on [pip installing](https://pypi.org/project/insta-scrape/) or [git cloning](https://github.com/chris-greening/instascrape) `instascrape` :smile:

Now let's jump right in! To start, we'll `import` the `Profile` scraper and load the data from Instagram:

```python
from instascrape import Profile 
chris = Profile('chris_greening')
chris.scrape()
recent_posts = chris.get_recent_posts()
```

Out of the box, `instascrape` does not render any JavaScript so the only posts we get are the 12 most recent (it's built with flexibility in mind however and can be extended with [`selenium`](https://selenium-python.readthedocs.io/) or similar)

---

# Organizing the data

Now that we have the data, let's create a `list` of `dict`'s that can easily be built into a [`pandas.DataFrame`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html)

```python
import pandas as pd 

posts_data = [post.to_dict() for post in recent_posts]
posts_df = pd.DataFrame(posts_data)
print(posts_df[['upload_date', 'comments', 'likes']])
```
which gives us 
```python
           upload_date  comments  likes
0  2020-10-16 14:39:41         8    119
1  2020-10-15 13:11:42        21    165
2  2020-10-14 12:36:21        16    150
3  2020-09-28 12:17:21         6    164
4  2020-09-27 09:27:00        14    210
5  2020-09-26 11:38:27        16    217
6  2020-09-25 10:18:28        17    227
7  2020-09-24 11:01:04        20    239
8  2020-09-17 17:49:18        15    279
9  2020-09-14 10:05:24        14    316
10 2020-09-09 10:24:17        13    244
11 2020-09-08 09:06:05        33    393
```

---

# Visualizing the data

Awesome! Now we can get to visualizing our data and see how the page is doing:
```python
import matplotlib.pyplot as plt 

plt.style.use('seaborn-darkgrid')      # Stylistic change

plt.scatter(df.upload_date, df.likes)  # Plot the data
plt.xlabel('Upload Date')              # Write labels
plt.ylabel('Likes')
plt.title('@chris_greening Likes per Post')
plt.show()                             # Show graph 
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1d56refxz95611sztaj8.png)

And that's it! As we can see, my Instagram is in fact trending downwards, yayyyy!... :sweat_smile: 

If you wanted to go further, you could use libraries such as [`scikit-learn`](https://scikit-learn.org/stable/) and [`selenium`](https://selenium-python.readthedocs.io/) to extend `instascrape` and fit regressors to dynamically loaded data for a more comprehensive visualization as shown below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/v30nkbevl02l2y1zt8zf.png)

Let me know your thoughts in the comments below or even better, check out the repo on Github and contribute! 

{% github chris-greening/instascrape %} 
{% embed https://www.christophergreening.com %}