如何使用 tweepy 库在 Twitter 上获取一个人的朋友和关注者?
How to get a person's friends and followers on Twitter using the tweepy library?
如果我从 (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'
如何解决?
Meu = []
def getting_friends_follwers():
# Get list of followers and following for group of users tweepy
f = open("amigos.txt","w")
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()
您收到该错误是因为您将项目传递给 "sorted" 函数,该函数试图对这些 "User" 对象进行排序,但由于没有关于如何 "sort" tweepy 用户对象。
如果您删除 "sorted",则该程序可以正常运行。
此外,您在调用该函数之前关闭了文件。我建议您使用 "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)
如果我从 (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'
如何解决?
Meu = []
def getting_friends_follwers():
# Get list of followers and following for group of users tweepy
f = open("amigos.txt","w")
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()
您收到该错误是因为您将项目传递给 "sorted" 函数,该函数试图对这些 "User" 对象进行排序,但由于没有关于如何 "sort" tweepy 用户对象。
如果您删除 "sorted",则该程序可以正常运行。
此外,您在调用该函数之前关闭了文件。我建议您使用 "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)