Amazing-Python-Scripts

Форк
0
149 строк · 4.2 Кб
1
from selenium import webdriver
2
from getpass import getpass
3
import time
4

5
# Class for the bot
6

7

8
class InstaBot:
9

10
    # Initializes bot
11
    def __init__(self):
12
        self.username = input('Enter your username:')
13
        self.pw = getpass('Enter your password(will NOT appear as you type):')
14
        self.PATH = r"C:\Program Files (x86)\chromedriver.exe"
15
        self.driver = webdriver.Chrome(self.PATH)
16

17
    # Starts Instagram
18
    def start(self):
19
        self.driver.get('https://www.instagram.com/')
20
        time.sleep(2)
21
        return
22

23
    # Logs into your account, also closes various dialogue boxes that open on
24
    # the way
25
    def login(self):
26

27
        user_field = self.driver.find_element_by_xpath(
28
            '//*[@id="loginForm"]/div/div[1]/div/label/input')
29
        pw_field = self.driver.find_element_by_xpath(
30
            '//*[@id="loginForm"]/div/div[2]/div/label/input')
31
        login_button = self.driver.find_element_by_xpath(
32
            '//*[@id="loginForm"]/div/div[3]/button/div')
33
        user_field.send_keys(self.username)
34
        pw_field.send_keys(self.pw)
35
        login_button.click()
36
        time.sleep(2.5)
37
        not_now1 = self.driver.find_element_by_xpath(
38
            '//*[@id="react-root"]/section/main/div/div/div/div/button')
39
        not_now1.click()
40
        time.sleep(2)
41
        not_now2 = self.driver.find_element_by_xpath(
42
            '/html/body/div[4]/div/div/div/div[3]/button[2]')
43
        not_now2.click()
44
        time.sleep(1)
45
        return
46

47
    # Opens your profile
48
    def open_profile(self):
49
        profile_link = self.driver.find_element_by_xpath(
50
            '//*[@id="react-root"]/section/main/section/div[3]'
51
            '/div[1]/div/div[2]/div[1]/a')
52
        profile_link.click()
53
        time.sleep(2)
54
        return
55

56
    # Opens the list of the people you follow
57
    def open_following(self):
58
        following_link = self.driver.find_element_by_xpath(
59
            '/html/body/div[1]/section/main/div/header/section/ul/li[3]/a')
60
        following_link.click()
61
        return
62

63
    # Gets the list of the people you follow
64
    def get_following(self):
65
        xpath = '/html/body/div[4]/div/div/div[2]'
66
        self.following = self.scroll_list(xpath)
67
        return
68

69
    # Opens the link to 'Followers'
70
    def open_followers(self):
71
        followers_link = self.driver.find_element_by_xpath(
72
            '//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a')
73
        followers_link.click()
74
        return
75

76
    # Gets the list of followers
77
    def get_followers(self):
78
        xpath = '/html/body/div[4]/div/div/div[2]'
79
        self.followers = self.scroll_list(xpath)
80
        return
81

82
    # Scrolls a scroll box and retrieves their names
83
    def scroll_list(self, xpath):
84

85
        time.sleep(2)
86
        scroll_box = self.driver.find_element_by_xpath(xpath)
87
        last_ht, ht = 0, 1
88

89
        # Keep scrolling till you can't go down any further
90
        while last_ht != ht:
91
            last_ht = ht
92
            time.sleep(1)
93
            ht = self.driver.execute_script(
94
                """
95
                arguments[0].scrollTo(0, arguments[0].scrollHeight);
96
                return arguments[0].scrollHeight;
97
                """, scroll_box)
98

99
        # Gets the list of accounts
100
        links = scroll_box.find_elements_by_tag_name('a')
101
        names = [name.text for name in links if name.text != '']
102

103
        # Closes the box
104
        close_btn = self.driver.find_element_by_xpath(
105
            '/html/body/div[4]/div/div/div[1]/div/div[2]/button/div')
106
        close_btn.click()
107

108
        return names
109

110
    # Prints the list of people you follow who don't follow you back in
111
    # terminal
112
    def get_unfollowers(self):
113

114
        self.unfollowers = [
115
            x for x in self.following if x not in self.followers
116
        ]
117
        for name in self.unfollowers:
118
            print(name)
119
        return
120

121
    # Closes the driver
122
    def close(self):
123
        self.driver.quit()
124
        return
125

126

127
def main():
128

129
    # Bot method calls
130
    bb8 = InstaBot()
131

132
    bb8.start()
133

134
    bb8.login()
135

136
    bb8.open_profile()
137
    bb8.open_following()
138

139
    bb8.get_following()
140
    bb8.open_followers()
141

142
    bb8.get_followers()
143
    bb8.get_unfollowers()
144

145
    bb8.close()
146

147

148
if __name__ == '__main__':
149
    main()
150

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.