如果我从(cursor2.items(100((中删除值100,下面的getting_friends_follwers()
函数就会工作。我的目标是获得这些名字(追随者和朋友(,并将它们保存在"amigos.txt"文件中。
问题是:screen_name
这个名字有大量的朋友和追随者,因此,推特关闭了连接。我曾想过尝试捕获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"对象进行排序,但它无法做到这一点,因为没有关于如何对花呢用户对象进行"排序"的说明。
如果你去掉"排序",那么这个程序就会正常工作。
此外,在调用函数之前,您可以关闭文件。我建议您使用"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)