简体   繁体   中英

Instagram Bot with Selenium

Trying to create a instagram like bot. It opens firebox browser correctly, find instagram and does the login in correctly. It also searches for the hashtags i enter and browse through, however it does not like the photos

    def like_photo(self, hashtag):
        driver = self.driver
        driver.get("https://www.instagram.com/explore/tags/"+ hashtag +"/")
        time.sleep(2)
        for i in range (1,3):
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            time.sleep(2)

        hrefs = driver.find_elements_by_tag_name('a')
        pic_hrefs = [elem.get_attribute('href') for elem in hrefs]
        pic_hrefs = [href for href in pic_hrefs if hashtag in href]
        print(hashtag + ' photos: ' + str(len(pic_hrefs)))

        for pic_href in pic_hrefs:
            driver.get(pic_href)
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            try:
                driver.find_element_by_link_text("Like").click()
                time.sleep(18)
            except Exception as e:
                time.sleep(2)


tryIG = InstagramBot("ueracct", "12345")
tryIG.login()
tryIG.like_photo('tag1')

Try searching for the photo and just double tap it.

elem = driver.find_elements_by_xpath("//div[contains(@class, '_9AhH0')]")[0]

actions = ActionChains(driver)
actions.double_click(elem)
actions.perform()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM