2015-06-23 32 views
2

我試圖用Tweepy從與像500K追隨者的賬戶讓追隨者的完整列表,我有一個讓我對小賬戶的用戶名,如在100碼的追隨者大名單,但是如果我得到一個甚至像110個追隨者那樣的人,那麼這是行不通的。任何幫助搞清楚如何使它與更大的數字工作,非常感謝!如何獲得Tweepy

下面的代碼我現在所擁有的:

import tweepy 
import time 

key1 = "..." 
key2 = "..." 
key3 = "..." 
key4 = "..." 

accountvar = raw_input("Account name: ") 

auth = tweepy.OAuthHandler(key1, key2) 
auth.set_access_token(key3, key4) 

api = tweepy.API(auth) 

ids = [] 
for page in tweepy.Cursor(api.followers_ids, screen_name=accountvar).pages(): 
    ids.extend(page) 
    time.sleep(60) 

users = api.lookup_users(user_ids=ids) 
for u in users: 
    print u.screen_name 

我不斷收到的錯誤是:

Traceback (most recent call last): 
    File "test.py", line 24, in <module> 
    users = api.lookup_users(user_ids=ids) 
    File "/Library/Python/2.7/site-packages/tweepy/api.py", line 321, in lookup_users 
    return self._lookup_users(post_data=post_data) 
    File "/Library/Python/2.7/site-packages/tweepy/binder.py", line 239, in _call 
    return method.execute() 
    File "/Library/Python/2.7/site-packages/tweepy/binder.py", line 223, in execute 
    raise TweepError(error_msg, resp) 
tweepy.error.TweepError: [{u'message': u'Too many terms specified in query.', u'code': 18}] 

我已經看了大量關於這類問題的其他問題,但沒有我能找到的解決方案適用於我,但如果有人有解決方案的鏈接,請發送給我!

+0

爲什麼你需要* *後的圖像?錯誤消息是**文本**,請使用代碼格式設置。 – jonrsharpe

回答

1

Twitter的API僅允許100個用戶在同一時刻被搜索。這就是爲什麼無論多少,你輸入到它,你會得到100 followers_id是給你的用戶數正確,但你被GET users/lookup

限制,你需要做的是通過每個迭代100什麼但保持在限制範圍內。

9

其實我想通了,所以我會在這裏發佈的解決方案僅供參考。

import tweepy 
import time 

key1 = "..." 
key2 = "..." 
key3 = "..." 
key4 = "..." 

accountvar = raw_input("Account name: ") 

auth = tweepy.OAuthHandler(key1, key2) 
auth.set_access_token(key3, key4) 

api = tweepy.API(auth) 

users = tweepy.Cursor(api.followers, screen_name=accountvar).items() 

while True: 
    try: 
     user = next(users) 
    except tweepy.TweepError: 
     time.sleep(60*15) 
     user = next(users) 
    except StopIteration: 
     break 
    print "@" + user.screen_name 

每隔300分鐘後停止15分鐘,然後繼續。這確保它不會遇到問題。這顯然需要年齡大的帳戶,但正如Leb提到的:

Twitter API只允許100個用戶一次搜索... [所以]你需要做的是迭代每100個用戶,但保持在速率限制內。

你基本上只需要離開程序運行,如果你想下一組。我不知道爲什麼我一次給300人,而不是100人,但正如我之前提到過的我的計劃,它早些時候也給了我100。

希望這有助於其他任何人有同樣的問題,因爲我,大喊答題節目環節以LEB提醒我關注的速率限制。

+1

我會考慮去稍微超過15分鐘,以去除您的計算機和Twitter API之間的任何可能的延遲。我沒有實際的數字,也許16?我不確定Twitter的'15分鐘'有多嚴格,'14:35'會好嗎?爲了安全起見,確保程序在干擾最小的情況下運行。 – Leb

4

要在此擴展:

您可以通過添加計數參數收穫每十五分鐘3000個用戶:

users = tweepy.Cursor(api.followers, screen_name=accountvar, count=200).items() 

這將調用Twitter API的15倍,按您的版本,但不是默認計數= 20,每個API調用將返回200(即你得到3000而不是300)。