Instagram: How to keep track of the follower growth of several accounts?

8 February 2020

How fast are your followers growing?

Scraping follower count from instagram profiles with python and Beautiful Soup.

Would you like to know how fast your instagram account is gaining followers? Maybe you are interested in knowing if other profiles are increasing their followerbase faster or slower than you. In this post you will find an automated way of answering these questions using python and Beautifulsoup.

Scraping an instagram account

Imagine you want to know how fast RosalĂ­a is gaining followers on instagram. First thing you do is accessing the URL of the account with the help of the requests module. If your response is 200 that means the access was succesful. Note that some pages will block this type of requests. See the case of LinkedIn.

			
url = 'https://www.instagram.com/rosalia.vt/' 
response = requests.get(url)
print(response)
			
		

For this specific solution you don't really need to use BeautifulSoup, but it is useful to understand the structure of the content you want to scrape. BeautifulSoup allows you to easily access different parts of an html page. In this case we are using soup.body.get_text() to get all text from the body.

			
soup = BeautifulSoup(response.text)
text = soup.body.get_text() #get all text from body.
			
		

Looking at the text from the body we quickly realise that the amount of followers is included inside an a tag. In the identificator of this tag it is written "edge_followed_by" just before the follower count we want to scrape.

A quick way to access this number is to split the text in two by the string "edge_followed_by" using .partition. The [2] selects the second part of the splited text, where the follower count is. Then we cut the string to start on the follower count. The last step is to use a second partition with "}" to remove anything after the number of followers.

				
fol_count = text.partition("edge_followed_by")[2][11:25] #split text just before followers appear.
followers = fol_count.partition("}")[0] #cut after the number of followers is finished.  
			
		

Start collecting the data

It is quite easy to generalise this method to other platforms and start automatizing the scraping. In a future post I'll show how to do this.

You can find the full code here.