collinsanele

Instagram Bot To Get Followers Written with Python 3

Dec 26th, 2019
9,463
0
Never
12
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.99 KB | None | 0 0
  1.  
  2. #Written by: [email protected]
  3.  
  4. from selenium import webdriver
  5. from selenium.webdriver.common.keys import Keys
  6. from selenium.webdriver.chrome.options import Options
  7. from selenium.webdriver.support.ui import WebDriverWait
  8. from selenium.webdriver.common.by import By
  9. from selenium.webdriver.support import expected_conditions as EC
  10. import time
  11. import random
  12. from tqdm import tqdm
  13.  
  14.  
  15. class Instagram:
  16.     def __init__(self, username, password, tags):
  17.        
  18.         """ NB tags should be list e.g tags=["Tech", "Fitness"]"""
  19.        
  20.         self.username = username
  21.         self.password = password
  22.         self.tags = tags
  23.         self.options = Options()
  24.         self.options.add_argument("--disable-notifications")
  25.         self.driver = webdriver.Chrome("chromedriver.exe", options=self.options)
  26.        
  27.     def login(self):
  28.         print("Working please wait...")
  29.        
  30.         try:
  31.             self.driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")  
  32.         except Exception:
  33.             return "Invalid Login Details Or Bad Network Connection"
  34.        
  35.         time.sleep(4)
  36.         username_input = self.driver.find_element_by_xpath("""//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[2]/div/label/input""")
  37.         password_input = self.driver.find_element_by_xpath("""//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[3]/div/label/input""")
  38.  
  39.         username_input.click()
  40.         username_input.clear()
  41.        
  42.         password_input.click()
  43.         password_input.clear()
  44.        
  45.         username_input.send_keys(self.username)
  46.         password_input.send_keys(self.password, Keys.ENTER)
  47.         time.sleep(4)
  48.        
  49.            
  50.        
  51.     def get_post_from_tags(self):
  52.         post_links = []
  53.         for tag in self.tags:
  54.             try:
  55.                 self.driver.get(f"https://www.instagram.com/explore/tags/{tag}/")
  56.                 time.sleep(3)
  57.                 sub_post_links = [link.get_attribute("href") for link in self.driver.find_elements_by_tag_name("a") if "/p/" in link.get_attribute("href")]
  58.                 post_links.extend(sub_post_links)
  59.                
  60.             except Exception:
  61.                 pass
  62.            
  63.         return post_links
  64.    
  65.    
  66.     def execute(self, all_comments):
  67.         self.login()
  68.         post_links = self.get_post_from_tags()
  69.        
  70.         for post_link in tqdm(post_links[0:]):
  71.             self.driver.get(post_link)
  72.             time.sleep(3)
  73.             self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  74.             time.sleep(2)
  75.            
  76.             try:
  77.                 self.comment(comments=all_comments)
  78.                
  79.             except Exception as e:
  80.                 print(e)
  81.                 return "Please ensure you are logged in with your correct credentials"
  82.            
  83.             time.sleep(random.randint(18, 28))
  84.            
  85.         print("Completed Successfully")
  86.  
  87.  
  88.     def comment(self, comments):
  89.         text_area = lambda:self.driver.find_element_by_tag_name("textarea")
  90.         text_area().click()
  91.         text_area().clear()
  92.        
  93.         comment = random.choice(comments)
  94.        
  95.         for letter in comment:
  96.             sleep_interval = random.randint(1, 7)/30
  97.             text_area().send_keys(letter)
  98.             time.sleep(sleep_interval)
  99.                
  100.         self.like()
  101.         text_area().send_keys(Keys.ENTER)
  102.                
  103.                
  104.                
  105.                
  106.     def like(self):
  107.         try:
  108.             like_btn = lambda:self.driver.find_element_by_xpath('//span[@class="glyphsSpriteHeart__outline__24__grey_9 u-__7"]')
  109.             self.driver.execute_script("arguments[0].click();", like_btn())
  110.            
  111.         except Exception as e:
  112.             pass
  113.    
  114.            
  115.        
  116. bot = Instagram(username="username", password="password", tags=["fitness", "lifestyle"])
  117.  
  118. bot.execute(all_comments=["Nice!", "Great Post", "Well Done", "Lovely"])
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment