我正在制作一个喜欢所有尚未被喜欢的帖子的机器人

时间:2019-07-27 21:02:24

标签: python selenium

问题在于它不喜欢帖子。

我尝试了标签名称之类的分散方法


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time


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

        # find all the heart links
        hrefs = driver.find_elements_by_xpath("//span[@aria-label='Synes godt om']")
        pic_hrefs = [elem.get_attribute('href') for elem in hrefs]
        pic_hrefs = [href for href in pic_hrefs]
        print(' Photos ' + str(len(pic_hrefs)))

        for _ in pic_hrefs:
            driver.get("https://www.instagram.com")
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            try:
                like_button = lambda: driver.find_elements_by_xpath("//span[@aria-label='Synes godt om']")
                like_button.click()
                time.sleep(18)
            except Exception as e:
                time.sleep(1)


nameIG = InstagramBot(username, password)
nameIG.login()
nameIG.like_photo()


就像任何帖子一样输出的只是照片4

以退出代码0结束的过程

1 个答案:

答案 0 :(得分:0)

exit code 0表示您的代码正在正常运行。但是,仍然存在问题。

要查看代码中是否存在实际错误,请更改异常操作。

    except Exception as e:
        print(e)  # shows actual error

尝试一下:

like_buttons = driver.find_elements_by_xpath(some_xpath_to_buttons)  # list of WebElements
for button in like_buttons:
    button.click()
    time.sleep(18)