This is the full code from the article "Instagram: How to keep track of the follower growth of several accounts?".

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Import used modules
import requests
from bs4 import BeautifulSoup
import time
import datetime
import pandas as pd
import numpy as np

# Select accounts to download count of followers:
accounts = ['dotcsv','rosalia.vt']

data = [] #empty vector to store data

for account in accounts: 
    
    # Request the url to instagram
    url = 'https://www.instagram.com/%s/' %account
    response = requests.get(url)
    print(response) # 200=succesful connection

    # Scrape
    soup = BeautifulSoup(response.text)
    text = soup.body.get_text() #get all text from body.
    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.  

    # Get the actual date
    date = datetime.datetime.now() 
    
    # Save results to vector 'data'
    data.append([date,followers,account])
    
    # Show results
    print('account:' + account )
    print('followers:' + followers)
    print('date: ' + str(datetime.datetime.now()))
    
    # Wait between requests to avoid being blocked
    time.sleep( np.random.uniform( low=0.5 , high=4.2 ) ) #add random seconds between calls to mimic human behaviour
    
#create pandas DataFrame with data:
instagramFollowers = pd.DataFrame(data, columns= ['date', 'followers', 'account'])
instagramFollowers.head() #show DataFrame

#save data to csv
dateformated = str(date)[:10] #format date to string
instagramFollowers.to_csv("instaFollowers%s.csv" % dateformated) #save data to a csv