-2
我正在複製書"21 recipes for mining Twitter"中的一個配方。 儘管我已經複製並粘貼了代碼,但我收到了一個我不明白的錯誤。我不明白錯誤:列表索引超出範圍
這裏去的代碼
import sys
import twitter
from recipe_make_twitter_request import make_twitter_request
import functools
SCREEN_NAME = sys.argv[1]
MAX_IDS = int(sys.argv[2])
if __name__ == '__main__':
# Not authenticating lowers your rate limit to 150 requests per hr.
# Authenticate to get 350 requests per hour.
t = twitter.Twitter(domain='api.twitter.com', api_version='1')
# You could call make_twitter_request(t, t.friends.ids, *args, **kw) or
# use functools to "partially bind" a new callable with these parameters
get_friends_ids = functools.partial(make_twitter_request, t, t.friends.ids)
# XXX: Ditto if you want to do the same thing to get followers...
# get_followers_ids = functools.partial(make_twitter_request, t, t.followers.ids)
cursor = -1
ids = []
while cursor != 0:
# Use make_twitter_request via the partially bound callable...
response = get_friends_ids(screen_name=SCREEN_NAME, cursor=cursor)
ids += response['ids']
cursor = response['next_cursor']
print >> sys.stderr, 'Fetched %i total ids for %s' % (len(ids), SCREEN_NAME)
# Consider storing the ids to disk during each iteration to provide an
# an additional layer of protection from exceptional circumstances
if len(ids) >= MAX_IDS:
break
# Do something useful with the ids like store them to disk...
print ids
當我運行它,我得到一個:
上SCREEN_NAMElist index out of range
。
在事實,sys.argv中剛剛1對應的腳本名稱項目:
sys.argv[1]
[u'/Users/massimo/Desktop/Twitter/recipe_get_friends_followers.py']
我的理解是,SCREEN_NAME應該包含我想從提取的追隨者的Twitter的名稱。 MAX_IDS應該是我想要獲得的最多關注者人數。
但是,我該如何指定這樣的參數?目前,他們不應該包含在argv列表中。
你是如何運行此腳本的最大數目取代
some_user
與期望的Twitter帳號,並max_ids
? –你應該從終端運行腳本爲'python script.py%screen_name%%max_ids%' –
如果'sys.argv'只有一個項目,它是腳本名稱,它將不會有一個元素'1'_ 。您的示例輸出顯示'sys.argv [1]'是腳本名稱,因此無法幫助您解決問題併爲您提供幫助。 – TigerhawkT3