following_button = browser.find_element_by_xpath("/html/body/div[1]/section/main/div/header/section/ul/li[3]/a")
following_button.click()
time.sleep(random.randrange(3, 5))
following_ul = browser.find_element_by_xpath("/html/body/div[4]/div/div/div[2]")
try:
following_urls = []
print("Запускаем сбор подписок")
for i in range(1, following_loops_count + 1):
browser.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", following_ul)
time.sleep(random.randrange(2, 4))
print(f"Итерация #{i}")
all_urls_div = following_ul.find_elements_by_tag_name("li")
for url in all_urls_div:
url = url.find_element_by_tag_name("a").get_attribute("href")
following_urls.append(url)
# сохраняем всех подписок пользователя в файл
with open(f"{username}_following_list.txt", "a") as following_file:
for link in following_urls:
following_file.write(link + "\n")
"""Сравниваем два списка, если пользователь есть в подписках, но его нет в подписчиках,
заносим его в отдельный список"""
count = 0
unfollow_list = []
for user in following_urls:
if user not in followers_urls:
count += 1
unfollow_list.append(user)
print(f"Нужно отписаться от {count} пользователей")
# сохраняем всех от кого нужно отписаться в файл
with open(f"{username}_unfollow_list.txt", "a") as unfollow_file:
for user in unfollow_list:
unfollow_file.write(user + "\n")
print("Запускаем отписку...")
time.sleep(2)
# заходим к каждому пользователю на страницу и отписываемся
with open(f"{username}_unfollow_list.txt") as unfollow_file:
unfollow_users_list = unfollow_file.readlines()
unfollow_users_list = [row.strip() for row in unfollow_users_list]
try:
count = len(unfollow_users_list)
for user_url in unfollow_users_list:
browser.get(user_url)
time.sleep(random.randrange(4, 6))
# кнопка отписки
unfollow_button = browser.find_element_by_xpath("/html/body/div[1]/section/main/div/header/section/div[1]/div[1]/div/div[2]/div/span/span[1]/button")
unfollow_button.click()
time.sleep(random.randrange(4, 6))
# подтверждение отписки
unfollow_button_confirm = browser.find_element_by_xpath("/html/body/div[4]/div/div/div/div[3]/button[1]")
unfollow_button_confirm.click()
print(f"Отписались от {user_url}")
count -= 1
print(f"Осталось отписаться от: {count} пользователей")
# time.sleep(random.randrange(120, 130))
time.sleep(random.randrange(4, 6))
except Exception as ex:
print(ex)
self.close_browser()
except Exception as ex:
print(ex)
self.close_browser()
time.sleep(random.randrange(4, 6))
self.close_browser()
for user, user_data in users_settings_dict.items():
username = user_data['login']
password = user_data['password']
# window_size = user_data['window_size']
my_bot = InstagramBot(username, password)
my_bot.login()
# my_bot.close_browser()
# my_bot.send_direct_message(direct_users_list, "Hey! How's it going?", "/home/cain/PycharmProjects/instagram_bot/lesson_6/img1.jpg")
# my_bot.get_all_followers('username')
my_bot.smart_unsubscribe("username")
правила читать не учили?
Обсуждают сегодня