首页 > 解决方案 > 随机选择一种方法?属性错误 - Python

问题描述

使用 instapy,我同时使用了以下和取消关注的方法。为了将程序运行所需的时间减半,我试图让程序在每次运行时只关注或取消关注。我不必手动注释掉一种或另一种方法,而是尝试将两者都放在一个列表中,并使用 random.choice() 来选择一个或另一个来运行。

account_list = ['dji_official' , 'drone', 'dronenerds', 'dronepals']
first = session.follow_likers([random.sample(account_list, 2)], photos_grab_amount = 2, follow_likers_per_photo = 15, randomize=True, sleep_delay=25, interact=False)
# unfollow
second = session.unfollow_users(amount=40, allFollowing=True, style="FIFO", unfollow_after=12*60*60, sleep_delay=15)

action_list = [first, second]
random.choice(action_list)

此代码返回错误“'list' object has no attribute 'replace'”不确定它在哪里/为什么会引发此错误。有简单的解决方法吗?还是我最好尝试一个功能?

标签: pythonlistmethodsattributesinstapy

解决方案


这看起来对if. 你可以像这样构造你的代码:

account_list = ['dji_official' , 'drone', 'dronenerds', 'dronepals']
action_list = [1, 2]
option = random.choice(action_list)
if option == 1:
    session.follow_likers([random.sample(account_list, 2)], photos_grab_amount = 2, follow_likers_per_photo = 15, randomize=True, sleep_delay=25, interact=False)
elif option == 2:
    session.unfollow_users(amount=40, allFollowing=True, style="FIFO", unfollow_after=12*60*60, sleep_delay=15)

请注意,正如上面所指出的,您需要将运行的结果存储random.choice在一个变量中(我称之为它option),以便您可以对它做一些事情,比如将它检查到一个if.


推荐阅读