Instagram的Selenium代码无法加载图像并停止工作

2024-04-27 00:11:22 发布

您现在位置:Python中文网/ 问答频道 /正文

Instagram喜欢的机器人程序代码工作完美,直到图像无法加载到Instagram上。然后它停止工作,我需要一个if语句来处理这样的场景

我不知道为什么,但有时Instagram不打开图片。它可以在第四个或第五十个

我只需要一些代码: 如果Instagram找不到like按钮,请按next按钮。到目前为止,我掌握的情况如下:

def likePhotos(self,amount):
    bot = self.bot
    bot.find_element_by_class_name('v1Nh3').click()
    i = 1
    while i <= amount:
        time.sleep(2)
        bot.find_element_by_class_name('fr66n').click()
        time.sleep(1)
        bot.find_element_by_class_name('coreSpriteRightPaginationArrow').click()
        time.sleep(1)
    i += 1

Tags: nameselfbytimebot机器人sleepelement
1条回答
网友
1楼 · 发布于 2024-04-27 00:11:22

您可以使用tryexcept

def likePhotos(self,amount):
bot = self.bot
bot.find_element_by_class_name('v1Nh3').click()
i = 1
while i <= amount:
    time.sleep(2)
    try:
        bot.find_element_by_class_name('fr66n').click()
        time.sleep(1)
    except:
        # do somthing if error occurs
    bot.find_element_by_class_name('coreSpriteRightPaginationArrow').click()
    time.sleep(1)
i += 1

相关问题 更多 >