python - 随机选择一种方法?属性错误 - 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'”不确定它在哪里/为什么会引发此错误。有简单的解决方法吗?还是我最好尝试一个功能?
解决方案
这看起来对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
.
推荐阅读
- python - 在使用熊猫创建带有标签的数据框时,它返回一个空数据框
- unity3d - Unity 无光照着色器中的无效下标“w”
- javascript - “TypeError:docFile.makeCopy 不是函数”
- python - 如何将一个数据框映射到另一个数据框以获取横截面面板数据?
- java - 当我用 Hive 运行 Show Databases 时,报错
- azure - 无法使用 E5 开发租户的条件访问策略分配身份验证上下文
- php - 即使使用 csrf_token,表单提交也会发生 Laravel 419 错误
- typescript - 柏树 - 加入黄瓜
- android-studio - Jetpack Compose 预览版在带有补丁 1 的北极狐中停止工作
- flutter - Flutter Web 应用程序无法加载 Firebase 图像