如何使用tweepy库在Twitter上获得一个人的朋友和关注者?

问题描述 投票:0回答:1

如果我从(cursor2.items(100))中删除值100,下面的getting_friends_follwers()函数将起作用。我的目标是获取这些名称(关注者和朋友)并将其保存在“ amigos.txt”文件中。

问题:名称screen_name有大量的朋友和关注者,因此,该连接已被Twitter关闭。我考虑过尝试捕获100个名称中的100个(因此在对cursor2的调用中值为100),但是发生以下错误:

builtins.TypeError: '<' not supported between instances of 'User' and 'User'

如何解决?

f = open("amigos.txt","w")
Meu = []
def getting_friends_follwers():
    # Get list of followers and following for group of users tweepy
    cursor = tweepy.Cursor(api.friends, screen_name="Carlos")
    cursor2 = tweepy.Cursor(api.followers, screen_name="Carlos")
##    for user in cursor.items():
##        print('friend: ' + user.screen_name)

    for user in sorted(cursor2.items(100)):###funciona se eu tirar este valor!!!
         f.write(str(user.screen_name)+ "\n")


         print('follower: ' + user.screen_name)

f.close()
getting_friends_follwers()
python python-3.x tweepy
1个回答
0
投票

您会收到此错误,因为您正在将项目传递给“已排序”功能,该功能正在尝试对那些“用户”对象进行排序,但由于没有有关如何“对”蠕虫用户进行“排序”的说明,因此无法执行此操作对象。

如果删除“已排序”,则该程序可以正常运行。

此外,您在调用函数之前关闭文件。我建议您使用“ with open”语法来确保文件正确关闭。

您可以这样编写代码:

def getting_friends_follwers(file):
    # Get list of followers and following for group of users tweepy
    cursor = tweepy.Cursor(api.friends, screen_name="Carlos")
    cursor2 = tweepy.Cursor(api.followers, screen_name="Carlos")
##    for user in cursor.items():
##        print('friend: ' + user.screen_name)

    for user in cursor2.items(100):###funciona se eu tirar este valor!!!
         file.write(str(user.screen_name)+ "\n")
         print('follower: ' + user.screen_name)

with open("amigos.txt", "w") as file:
    getting_friends_follwers(file)

© www.soinside.com 2019 - 2024. All rights reserved.