Instagram like bot - インスタグラム自動いいねボット

  1. from selenium import webdriver
  2. from selenium.webdriver.common.keys import Keys
  3. import time
  4. import random
  5. import sys
  6. from settings import user_login, user_password
  7.  
  8. # Paste.jp Instagram like bot
  9.  
  10.  
  11. def print_same_line(text):
  12.     sys.stdout.write('\r')
  13.     sys.stdout.flush()
  14.     sys.stdout.write(text)
  15.     sys.stdout.flush()
  16.  
  17.  
  18. class InstagramBot:
  19.  
  20.     def __init__(self, username, password):
  21.         self.username = username
  22.         self.password = password
  23.         self.driver = webdriver.Chrome()
  24.  
  25.     def closeBrowser(self):
  26.         self.driver.close()
  27.  
  28.     def login(self):
  29.         driver = self.driver
  30.         driver.get("https://www.instagram.com/")
  31.         time.sleep(2)
  32.         login_button = driver.find_element_by_xpath("//a[@href='/accounts/login/?source=auth_switcher']")
  33.         login_button.click()
  34.         time.sleep(2)
  35.         user_name_elem = driver.find_element_by_xpath("//input[@name='username']")
  36.         user_name_elem.clear()
  37.         user_name_elem.send_keys(self.username)
  38.         password_elem = driver.find_element_by_xpath("//input[@name='password']")
  39.         password_elem.clear()
  40.         password_elem.send_keys(self.password)
  41.         password_elem.send_keys(Keys.RETURN)
  42.         time.sleep(2)
  43.  
  44.     def like_photo(self, tag):
  45.         driver = self.driver
  46.         driver.get("https://www.instagram.com/explore/tags/" + tag + "/")
  47.         time.sleep(2)
  48.  
  49.         pic_hrefs = []
  50.         for i in range(1, 2):
  51.             try:
  52.                 driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  53.                 time.sleep(2)
  54.  
  55.                 hrefs_in_view = driver.find_elements_by_tag_name('a')
  56.                 hrefs_in_view = [elem.get_attribute('href') for elem in hrefs_in_view if '.com/p/' in elem.get_attribute('href')]
  57.                 [pic_hrefs.append(href) for href in hrefs_in_view if href not in pic_hrefs]
  58.             except Exception:
  59.                 continue
  60.  
  61.         unique_photos = len(pic_hrefs)
  62.         for pic_href in pic_hrefs:
  63.             driver.get(pic_href)
  64.             time.sleep(2)
  65.             driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  66.             try:
  67.                 time.sleep(random.randint(2, 4))
  68.                 like_button = lambda: driver.find_element_by_xpath('//span[@aria-label="Нравится"]').click()
  69.                 like_button().click()
  70.                 for second in reversed(range(0, random.randint(18, 28))):
  71.                     print_same_line("#" + tag + ': unique photos left: ' + str(unique_photos)
  72.                                     + " | Sleeping " + str(second))
  73.                     time.sleep(1)
  74.             except Exception as e:
  75.                 time.sleep(2)
  76.             unique_photos -= 1
  77.  
  78.  
  79. bot = InstagramBot(user_login, user_password)
  80. bot.login()
  81.  
  82. tags = ['beer', 'workout', 'like', 'trip', 'motivation', 'travel', 'sport', 'roadtothedream', 'insta']
  83.  
  84. while True:
  85.     try:
  86.         tag = random.choice(tags)
  87.         bot.like_photo(tag)
  88.     except Exception:
  89.         bot.closeBrowser()
  90.         time.sleep(60)
  91.         InstagramBot(user_login, user_password)
  92.         bot.login()

連絡先: info@paste.jp
Created by Paste.jp - v7.0