Instgram Bot与硒

时间:2018-11-18 21:08:14

标签: python selenium-webdriver

尝试创建像bot这样的insagram。它会正确打开firebox浏览器,找到instagram并正确登录。它还搜索我输入并浏览的主题标签,但是不喜欢照片

    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')

1 个答案:

答案 0 :(得分:0)

尝试搜索照片,然后双击它。

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

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