Skip to content

Commit 7264b22

Browse files
authored
Merge pull request HarshCasper#221 from aastha985/insta_follower_following_scraper
Add Script to scrape Instagram User's Followers and Following and store them in a csv file.
2 parents f0aea5b + 3a1b053 commit 7264b22

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Instagram User's Followers and Following Scraper
2+
This script scraps an Instagram user's following and followers and stores them in a csv file.
3+
4+
### Dependencies
5+
1. Python3
6+
1. selenium
7+
1. pandas
8+
1. chrome driver
9+
10+
### Running the script
11+
1. Change the path to the path of chrome driver in your system.
12+
1. `py main.py`
13+
1. Enter your instagram username ( Viewing a user's followers and following requires us to login to instagram)
14+
1. Enter your instagram password
15+
1. Enter the user's instagram username to scrap their followers and following
16+
1. The user's followers and following will be stored in a csv file <username>.csv
17+
18+
### Demo
19+
[Watch the demo video](https://drive.google.com/file/d/165eaVN8FsYdt5E2Ztkfrhfvl3DEvAT10/view?usp=sharing)
20+
21+
### Disclaimer
22+
This script is developed for educational purposes only. Any misuse of this script is not allowed. You are responsible for your own actions.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
from selenium import webdriver
2+
from time import sleep
3+
from selenium.webdriver.common.keys import Keys
4+
from selenium.common.exceptions import NoSuchElementException
5+
import pandas as pd
6+
7+
8+
def get_users(user_type, insta_id):
9+
'''Takes the user_type( a string named followers or following)
10+
and a username as argument and returns an array of the user's
11+
followers or following repectively'''
12+
13+
# select followers/following button
14+
button = browser.find_element_by_css_selector(
15+
"a[href='/"+insta_id+"/"+user_type+"/']")
16+
# count of followers/following
17+
no = (int)(button.text.strip().split()[0])
18+
# click on followers/following button to open dialog
19+
button.click()
20+
# for selecting the dialog
21+
users = browser.find_element_by_class_name(
22+
"PZuss")
23+
# for getting list of users
24+
users = users.find_elements_by_css_selector(
25+
"li")
26+
view = browser.find_element_by_class_name("isgrP")
27+
actionChain = webdriver.ActionChains(browser)
28+
# for handeling scroll event on dialog
29+
actionChain.context_click(on_element=view)
30+
# no of followers/following in the list
31+
n = len(users)
32+
# while number of followers in the list is less than total number of followers of the user
33+
while(n < no):
34+
# for scrolling down
35+
actionChain.key_down(Keys.SPACE).key_up(
36+
Keys.SPACE).perform()
37+
# selecting users
38+
users = browser.find_element_by_class_name("PZuss")
39+
# updating the number of users in the list
40+
users = users.find_elements_by_css_selector("li")
41+
n = len(users)
42+
# extracting text
43+
for j in range(len(users)):
44+
users[j] = users[j].text
45+
browser.get("https://www.instagram.com/"+insta_id)
46+
return users
47+
48+
49+
def convert_to_csv(followers, following, insta_id):
50+
'''Takes arrays of followers and following and a username
51+
as arguments and creates <username>.csv file to store the data'''
52+
53+
final_arr = []
54+
for i in range(0, max(len(followers), len(following))):
55+
follower_account = ""
56+
follower_name = ""
57+
following_account = ""
58+
following_name = ""
59+
if(i < len(followers)):
60+
follower_account = followers[i].split("\n")[0]
61+
follower_name = followers[i].split("\n")[1]
62+
if(i < len(following)):
63+
following_account = following[i].split("\n")[1]
64+
following_name = following[i].split("\n")[0]
65+
user = {
66+
"Follower Account": follower_account,
67+
"Follower Name": follower_name,
68+
"Following Account": following_account,
69+
"Following Name": following_name,
70+
}
71+
final_arr.append(user)
72+
# convert to data frame
73+
df = pd.DataFrame(final_arr)
74+
# convert to csv
75+
df.to_csv(insta_id+".csv", index=None)
76+
77+
78+
if __name__ == "__main__":
79+
80+
username = input("Enter your Instagram username: ")
81+
password = input("Enter your Instagram password: ")
82+
insta_id = input("Enter user's Instagram username for scraping: ")
83+
84+
# path to chromedriver
85+
PATH = "C:\Program Files (x86)\chromedriver.exe"
86+
browser = webdriver.Chrome(PATH)
87+
browser.implicitly_wait(5)
88+
browser.get("https://www.instagram.com/")
89+
90+
username_input = browser.find_element_by_css_selector(
91+
"input[name='username']")
92+
password_input = browser.find_element_by_css_selector(
93+
"input[name='password']")
94+
95+
# enters username and password
96+
username_input.send_keys(username)
97+
password_input.send_keys(password)
98+
99+
# click on submit button
100+
login_button = browser.find_element_by_xpath("//button[@type='submit']")
101+
login_button.click()
102+
103+
# save password dialog
104+
not_now_button = browser.find_element_by_xpath(
105+
"//button[text()='Not Now']")
106+
not_now_button.click()
107+
sleep(2)
108+
109+
# get user's instagram page
110+
browser.get("https://www.instagram.com/"+insta_id)
111+
sleep(2)
112+
113+
try:
114+
arr = ["followers", "following"]
115+
followers = get_users("followers", insta_id)
116+
following = get_users("following", insta_id)
117+
convert_to_csv(followers, following, insta_id)
118+
except NoSuchElementException:
119+
print("Invalid Account/ Private Account")
120+
pass

0 commit comments

Comments
 (0)