【问题标题】:Stacked in a loop creating friendship with tweepy堆叠在一个循环中,与 tweepy 建立友谊
【发布时间】:2019-05-12 14:31:15
【问题描述】:

所以我试图关注用户,但问题是它适用于每个用户,除了我在 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 中停止并且不会退出执行,问题出在哪里?

【问题讨论】:

  • “不退出执行”是什么意思?
  • 我的意思是它遵循 javi1597 之前的所有帐户,当它到达这个帐户时,它就像 api.createfriendship 中的“执行”一样
  • 可能有其他东西阻止该功能完成吗?例如,api 方法之一?如果createFriends 没有为 javi1597 打印“创建的友谊”,那么它正忙于api
  • 就是这样,但我不明白为什么,因为它适用于所有其他用户
  • 嗯,您需要查看api 代码是否有问题。

标签: python twitter tweepy friend-function twitter-follow


【解决方案1】:

我认为您应该使用变量“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 之前将其删除。只是一个想法。

【讨论】:

  • to_follow 是一个txt,里面包含了我想关注的所有用户,然后当他们有\n
  • 是的,“to_follow.txt”是您的文件,但我说的是您尝试拆分的“to_follow()”方法/函数。因为您的 txt 文件被加载到变量“accounts”而不是“to_follow”中。
  • 对不起,我弄错了,我的代码里有你说的
  • 应该删除是什么意思,你建议我怎么做,我真的很新
  • 将“fuentes.append(account)”替换为“fuentes.append(strip(account))”。它应该删除空格和可能的文件结尾 (EOF)。
猜你喜欢
  • 1970-01-01
  • 2015-09-14
  • 2016-09-11
  • 2011-12-01
  • 2015-01-27
  • 1970-01-01
  • 2019-08-19
  • 1970-01-01
  • 2012-12-11
相关资源
最近更新 更多