Skip to content

Commit 535c23a

Browse files
authored
Merge pull request #135 from HaripriyaB/instagram_bot
Instagram bot that list all accounts that you follow, which don't follow back
2 parents 2a6b0e7 + 80bf971 commit 535c23a

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

Python/Instagram_Bot/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Instagram Bot
2+
The aim is to list all the instagram handles that you follow, that don't follow you back. The implementation is purely done using selenium automation strategy.
3+
4+
### Libraries used:
5+
* [Selenium](https://selenium-python.readthedocs.io/index.html) : A popular automation tool.
6+
* [webdriver-manager](https://pypi.org/project/webdriver-manager/) : Supporting package.
7+
8+
### Pre-requisites:
9+
### **Chromedriver has to be installed before executing the program**
10+
11+
`>> pip3 install selenium`
12+
13+
`>> pip3 install webdriver-manager`
14+
15+
### Usage:
16+
`>> python instagram_bot.py`
17+
18+
### I/O:
19+
20+
```
21+
Enter Username:$(username)
22+
23+
Enter Password:$(password)
24+
25+
26+
output:
27+
Total unFollowers : $(unfollowers)
28+
<List of unfollowers account ids comes below>
29+
```

Python/Instagram_Bot/instagram_bot.py

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import selenium
2+
from selenium import webdriver
3+
from webdriver_manager.chrome import ChromeDriverManager
4+
from time import sleep
5+
6+
username = ""
7+
password = ""
8+
driver = None
9+
10+
def open_instagram():
11+
# set url to driver
12+
driver.get("https://instagram.com")
13+
14+
def login():
15+
sleep(2)
16+
# find username field
17+
driver.find_element_by_xpath("//input[@name='username']").send_keys(username)
18+
# find password field
19+
driver.find_element_by_xpath("//input[@name='password']").send_keys(password)
20+
# find Log in button
21+
driver.find_element_by_xpath("//button[@type='submit']").click()
22+
sleep(5)
23+
# click on not now for saving password
24+
driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]").click()
25+
sleep(2)
26+
# click on not now for turning on notifications
27+
driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]").click()
28+
sleep(2)
29+
30+
def get_unfollowers():
31+
# go to profile page
32+
driver.find_element_by_xpath("//a[contains(@href,'{}')]".format(username)).click()
33+
sleep(5)
34+
# click on following
35+
driver.find_element_by_xpath("//a[contains(@href,'{}/following')]".format(username)).click()
36+
sleep(2)
37+
# Get the list of following accounts
38+
following = get_names(driver)
39+
# click on followers
40+
driver.find_element_by_xpath("//a[contains(@href,'{}/followers')]".format(username)).click()
41+
sleep(2)
42+
# Get the list of followers accounts
43+
followers = get_names(driver)
44+
# compare both lists to find the difference accounts
45+
not_following_back = [user for user in following if user not in followers]
46+
return not_following_back
47+
48+
def get_names():
49+
# keep scrolling till the end of scroll view
50+
scroll_box = driver.find_element_by_xpath("/html/body/div[4]/div/div/div[2]")
51+
last_ht, ht = 0, 1
52+
while last_ht != ht:
53+
last_ht = ht
54+
sleep(1)
55+
ht = driver.execute_script("""
56+
arguments[0].scrollTo(0, arguments[0].scrollHeight);
57+
return arguments[0].scrollHeight;
58+
""", scroll_box)
59+
links = scroll_box.find_elements_by_tag_name('a')
60+
names = [name.text for name in links if name.text != '']
61+
# close button
62+
driver.find_element_by_xpath("/html/body/div[4]/div/div/div[1]/div/div[2]/button").click()
63+
return names
64+
65+
def main():
66+
global username
67+
global password
68+
global driver
69+
# Input from users
70+
username=input("Enter username:")
71+
password=input("Enter password:")
72+
# configuring webdriver
73+
driver = webdriver.Chrome(ChromeDriverManager().install())
74+
# operations
75+
open_instagram()
76+
login()
77+
unfollowers = get_unfollowers()
78+
# output
79+
print("Total unFollowers : {}\n".format(len(unfollowers)))
80+
for i in unfollowers:
81+
print(i)
82+
print("\n")
83+
driver.close()
84+
85+
86+
if __name__ == "__main__":
87+
main()

0 commit comments

Comments
 (0)