我写了这个代码来获取Twitter帐户的追随者的完整列表使用Tweepy:如何获得并保存到文件Twitter帐户的追随者的完整列表,与Tweepy
# ... twitter connection and streaming
fulldf = pd.DataFrame()
line = {}
ids = []
try:
for page in tweepy.Cursor(api.followers_ids, screen_name="twittername").pages():
df = pd.DataFrame()
ids.extend(page)
try:
for i in ids:
user = api.get_user(i)
line = [{'id': user.id,
'Name': user.name,
'Statuses Count':user.statuses_count,
'Friends Count': user.friends_count,
'Screen Name':user.screen_name,
'Followers Count':user.followers_count,
'Location':user.location,
'Language':user.lang,
'Created at':user.created_at,
'Time zone':user.time_zone,
'Geo enable':user.geo_enabled,
'Description':user.description.encode(sys.stdout.encoding, errors='replace')}]
df = pd.DataFrame(line)
fulldf = fulldf.append(df)
del df
fulldf.to_csv('out.csv', sep=',', index=False)
print i ,len(ids)
except tweepy.TweepError:
time.sleep(60 * 15)
continue
except tweepy.TweepError as e2:
print "exception global block"
print e2.message[0]['code']
print e2.args[0][0]['code']
最后我只有1000线在csv文件中,将所有内容保存在内存(dataframe)并将其保存到相同的循环中并不是最佳解决方案。但至少我有一些工作,但没有得到15000名追随者中的1000人。
任何帮助,这将不胜感激。
有些机会是''例外全局块''打印? – asongtoruin
是的,我不是专家,所以我只想知道它在哪里发生。但这不是问题,我认为这是将数据保存到文件中的问题。 – lazurens
我认为这与你试图捕捉错误的方式有关。如果你当时没有答案,我今天晚上会看看它。 – asongtoruin