我是第一次使用tweepy twitter API,需要一些帮助...在下面的代码块中,我将循环访问用户 ID 列表(存储为"帐户"(,对于我使用的每个帐户,我使用 tweepy。光标查找他们关注的 ID 列表(他们的"朋友"(。我也检查与我关注的帐户(存储在"ids"中(的匹配项,然后将匹配项存储为数据帧。
我遇到的问题是我不断收到"达到速率限制"错误。请问我该如何避免这种情况?我假设有一种更聪明的方法可以做到这一点!
谢谢。
df_list = []
for account in accounts:
friends = []
for page in tweepy.Cursor(api.friends_ids, id=account).pages():
friends.extend(page)
friends = pd.Series(friends)
matches = friends[friends.isin(ids)]
d = {'friend_id' : account, 'common_friends' : matches}
matches = pd.DataFrame(data=d)
df_list.append(matches)
final_df = pd.concat(df_list)
您可以使用:
api = tweepy.API(auth, wait_on_rate_limit=True)
当您达到最大请求时,请停止并等待,直到可以再次发送请求。