下面的代码是提供给正在抓取朋友" (不是关注者)特定Twitter用户的列表。出于某种原因,使用" api.lookup_users"时出错。错误表示"查询"中指定的术语太多。理想情况下,我想刮掉关注者并输出带有屏幕名称(而不是ID)的csv。我也想要他们的描述,但除非有关于提取两条信息的建议,否则可以在单独的步骤中执行此操作。以下是我正在使用的代码:
import time
import tweepy
import csv
#Twitter API credentials
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""
auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
ids = []
for page in tweepy.Cursor(api.friends, screen_name="").pages():
ids.extend(page)
time.sleep(60)
print(len(ids))
users = api.lookup_users(user_ids=ids) #iterates through the list of users and prints them
for u in users:
print(u.screen_name)
答案 0 :(得分:0)
从你得到的错误来看,似乎你在api.lookup_users请求中一次放了太多的id。尝试将您的ID列表拆分为较小的部分,并为每个部分发出请求。
import time
import tweepy
import csv
#Twitter API credentials
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""
auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
ids = []
for page in tweepy.Cursor(api.friends, screen_name="").pages():
ids.extend(page)
time.sleep(60)
print(len(ids))
idChunks = [ids[i:i + 300] for i in range(0, len(ids), 300)]
users = []
for idChunk in idChunks:
try:
users.append(api.lookup_users(user_ids=idChunk))
except tweepy.error.RateLimitError:
print("RATE EXCEDED. SLEEPING FOR 16 MINUTES")
time.sleep(16*60)
users.append(api.lookup_users(user_ids=idChunk))
for u in users:
print(u.screen_name)
print(u.description)
此代码尚未经过测试,并且不会写入CSV,但它可以帮助您解决您遇到的错误。块的大小为300是完全随意的,如果太小(或太大)则调整它。