Skip to content

Commit 2bf30f8

Browse files
author
Haripriya Baskaran
committed
Instagram bot-list accounts that you follow but that don't follow back
1 parent 7eaf118 commit 2bf30f8

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

Python/Instagram_Bot/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
### **There is a file called `secrets.py` which contains username and password that has to be filled by the user in order to execute the program successfully.**
16+
17+
### Usage:
18+
`>> python instagram_bot.py`
19+
20+
21+
22+
23+
24+

Python/Instagram_Bot/instagram_bot.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import selenium
2+
from selenium import webdriver
3+
from webdriver_manager.chrome import ChromeDriverManager
4+
from time import sleep
5+
from secrets import username, password
6+
7+
def open_instagram(driver):
8+
driver.get("https://instagram.com")
9+
10+
def login(driver):
11+
sleep(2)
12+
driver.find_element_by_xpath("//input[@name='username']").send_keys(username)
13+
driver.find_element_by_xpath("//input[@name='password']").send_keys(password)
14+
driver.find_element_by_xpath("//button[@type='submit']").click()
15+
sleep(5)
16+
driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]").click()
17+
sleep(2)
18+
driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]").click()
19+
sleep(2)
20+
21+
def get_unfollowers(driver):
22+
driver.find_element_by_xpath("//a[contains(@href,'{}')]".format(username)).click()
23+
sleep(5)
24+
driver.find_element_by_xpath("//a[contains(@href,'{}/following')]".format(username)).click()
25+
sleep(2)
26+
following = get_names(driver)
27+
driver.find_element_by_xpath("//a[contains(@href,'{}/followers')]".format(username)).click()
28+
sleep(2)
29+
followers = get_names(driver)
30+
not_following_back = [user for user in following if user not in followers]
31+
return not_following_back
32+
33+
def get_names(driver):
34+
scroll_box = driver.find_element_by_xpath("/html/body/div[4]/div/div/div[2]")
35+
last_ht, ht = 0, 1
36+
while last_ht != ht:
37+
last_ht = ht
38+
sleep(1)
39+
ht = driver.execute_script("""
40+
arguments[0].scrollTo(0, arguments[0].scrollHeight);
41+
return arguments[0].scrollHeight;
42+
""", scroll_box)
43+
links = scroll_box.find_elements_by_tag_name('a')
44+
names = [name.text for name in links if name.text != '']
45+
# close button
46+
driver.find_element_by_xpath("/html/body/div[4]/div/div/div[1]/div/div[2]/button").click()
47+
return names
48+
49+
def main():
50+
driver = webdriver.Chrome(ChromeDriverManager().install())
51+
open_instagram(driver)
52+
login(driver)
53+
unfollowers = get_unfollowers(driver)
54+
print("Total unFollowers : {}\n".format(len(unfollowers)))
55+
for i in unfollowers:
56+
print(i)
57+
print("\n")
58+
driver.close()
59+
60+
61+
if __name__ == "__main__":
62+
main()

Python/Instagram_Bot/secrets.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
username = "<Enter username here>"
2+
password = "<Enter password here>"

0 commit comments

Comments
 (0)