python - 堆叠在一个循环中与 tweepy 建立友谊

标签 python twitter tweepy friend-function twitter-follow

所以我试图关注用户,但问题是它适用于每个用户,除了我的 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/

相关文章:

python - Tweepy:Twitter 错误响应:状态代码 = 500

python - 即时/动态创建模板

python - Python doctest中的多行字符串输出

使用 Twitter 凭据登录 Firebase Auth 时出现 iOS 错误

python - Twitter oauth 与flask_oauthlib,无法生成请求 token

python - 使用 Python Tweepy 从 Twitter 趋势主题数据中提取主题标签

Python正则表达式用于删除奇怪的字符

python - PySerial - 无法发送 STX

Python,PyGame 图像不显示

php - 连接 HTML 和 PHP 以创建图像链接