selenium webdriver |在social n上取消关注所有朋友

2024-05-20 14:17:55 发布

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

你知道为什么只有15个人参加吗 而不是全部?在

你需要一个Instagram帐户来运行这个脚本。在

例如:
如果您的帐户是chrome1 此脚本将用于
https://www.instagram.com/chrome1/following/

for ns in driver.find_elements_by_class_name("_6jvgy"):
    try:
        ns.find_element_by_class_name("_r4e4p").click() # unFollow button!!!

        # time.sleep(2) # the same as without sleep  

        unfollow_nick = ns.find_element_by_class_name("notranslate").get_attribute("title")
        print(unfollow_nick) # now: prints all, but really unfollow only 15. 

    except:
        pass

这个问题是前一个问题的延续: Scroll in Selenium Webdriver (Python)

运行所需的所有代码: https://ideone.com/wYjHW4


Tags: nameinhttps脚本comby帐户sleep
1条回答
网友
1楼 · 发布于 2024-05-20 14:17:55

正是请求的速度(连续单击Following按钮)导致Instagram服务器拒绝/忽略大多数请求。

在每个请求之前增加一些睡眠时间。

在代码中,导入了sleep方法。因此,直接使用sleep(2)而不是time.sleep,这会抛出异常并捕获它,并使用pass关键字绕过它,因此您不知道该异常。我建议打印异常,然后使用pass关键字。

for ns in driver.find_elements_by_class_name("_6jvgy"):
    try:
        ns.find_element_by_class_name("_r4e4p").click() # unFollow button!!!

        sleep(2) # works now 

        unfollow_nick = ns.find_element_by_class_name("notranslate").get_attribute("title")
        print(unfollow_nick) # now: prints all, but really unfollow only 15. 

    except:
        pass

相关问题 更多 >