所以我试图关注用户,但问题是它适用于每个用户,除了我的 to_follow.txt 中的最后一个用户:
Chile_Temblores
Aguas_Antof
costaneranorte_
onemichile
Edelaysen
Chilquinta600
CGE_Clientes
Frontel_
EnelClientesCL
javi1597
我使用的代码如下:
def createFriends(api):
accounts = open("to_follow.txt", "r")
friends = api.friends_ids()
print("friends:", friends)
follow = accounts().split('\n')
print (follow)
for account in follow:
if account not in follow:
api.destroy_friendship(account)
for account in follow:
if account not in friends:
print("account: ", account)
fuentes.append(account)
api.create_friendship(account)
print("friendship created")
print(fuentes)
accounts.close()
所以当我打印正在发生的事情时,它停止在 javi1597 并且不会退出执行,问题出在哪里?
最佳答案
我认为您应该使用变量“accounts”而不是使用文件名“to_follow”作为方法:
def createFriends(api):
accounts = open("to_follow.txt", "r")
friends = api.friends_ids()
print("friends:", friends)
print(accounts)
for account in accounts:
if account not in friends:
print("account: ", account)
fuentes.append(account)
api.create_friendship(account)
print("friendship created")
print(fuentes)
accounts.close()
否则我不明白函数 to_follow() 来自哪里以及为什么不使用创建的变量“accounts”。
编辑:我重构了你的代码。您不必拆分文件,但可以直接使用“for in”迭代行。
编辑2:当您尝试添加最后一个元素“javi1597”时,它可能还包含“文件结尾”,并且应该在将其传递到 API 之前将其删除。只是一个想法。
关于python - 堆叠在一个循环中与 tweepy 建立友谊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53726876/