-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