Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.2 kB
2
Indexable
Never
import time
from instagramUserInfo import username, password
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
class Instagram:
    def __init__(self, username, password):
        self.browser = webdriver.Chrome()
        self.username = username
        self.password = password
        self.followers = []
        
    def signIn(self):
        url = "https://www.instagram.com/"
        self.browser.get(url)
        time.sleep(2)
        username_input = self.browser.find_element(By.XPATH,'//*[@id="loginForm"]/div/div[1]/div/label/input')
        password_input = self.browser.find_element(By.XPATH,'//*[@id="loginForm"]/div/div[2]/div/label/input')

        username_input.send_keys(self.username)
        password_input.send_keys(self.password)

        time.sleep(2)

        self.browser.find_element(By.XPATH,'//*[@id="loginForm"]/div/div[3]/button').click()
        time.sleep(5)

    def getfollowers(self):
        self.browser.get(f'https://www.instagram.com/{self.username}/')
        time.sleep(5)
        element = self.browser.find_element(By.XPATH, '/html/body/div[2]/div/div/div[2]/div/div/div/div[1]/div[1]/div[2]/div[2]/section/main/div/header/section/ul/li[3]/a')
        href_value=element.get_attribute('href')
        self.browser.get(href_value)
        time.sleep(5)

 

    def getfollowers2(self):
        #dialog=self.browser.find_element(By.CSS_SELECTOR,'div[role=dialog] > div._aano > div > div > div')
        
        time.sleep(5)
        dialog= self.browser.find_element(By.CSS_SELECTOR, 'div._aano > div > div ')
        followers_list = dialog.find_elements(By.CSS_SELECTOR,'div._aano > div > div > div')
        time.sleep(5)
        count=len(dialog.find_elements(By.CSS_SELECTOR,'div._aano > div > div > div'))
        print(count)  
        #dialog2=self.browser.find_element(By.CSS_SELECTOR,'#mount_0_0_i\+ > div > div > div:nth-child(3) > div > div > div.x9f619.x1n2onr6.x1ja2u2z > div > div.x1uvtmcs.x4k7w5x.x1h91t0o.x1beo9mf.xaigb6o.x12ejxvf.x3igimt.xarpa2k.xedcshv.x1lytzrv.x1t2pt76.x7ja8zs.x1n2onr6.x1qrby5j.x1jfb8zj > div > div > div > div > div.x7r02ix.xf1ldfh.x131esax.xdajt7p.xxfnqb6.xb88tzc.xw2csxc.x1odjw0f.x5fp0pe')


        #action = webdriver.ActionChains(self.browser)
        

        #action.key_down(Keys.TAB).key_up(Keys.TAB).perform()
        #action.key_down(Keys.TAB).key_up(Keys.TAB).perform()
        miktar=1
        time.sleep(2)

        while True:            
            #self.browser.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', dialog2)
            js_code = '''
                        function scrollToBottom() {
                                const container = document.querySelector('div.aano');

                                if (container) {
                                    container.scrollTop = container.scrollHeight;
                                }
                                    }

                        scrollToBottom();
                        '''


            for _ in range(10):
                self.browser.execute_script(js_code)
                time.sleep(1)
            newCount= len(dialog.find_elements(By.CSS_SELECTOR,'div._aano > div > div > div'))
            time.sleep(1)
            if count != newCount:
                count = newCount
                print(f'Yeni Count{newCount}')
                time.sleep(2)
                
            
            else:
                break
         
        followers_list = dialog.find_elements(By.CSS_SELECTOR,'div._aano > div > div > div')
      
        for i in followers_list:
            i2=i.find_element(By.TAG_NAME , 'a').get_attribute('href')
            self.followers.append(i2)
            #self.followers.append(i.text.split('\n')[0])
          

ins = Instagram(username, password)
ins.signIn()
ins.getfollowers()
ins.getfollowers2()
print(ins.followers)