我寫了這個代碼來獲取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