我不明白错误:列表索引超出范围

时间:2016-09-23 14:48:44

标签: python twitter indexoutofboundsexception argv

我正在复制书籍"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_NAME上。

事实上,sys.argv只有一个对应于脚本名称的项目:

sys.argv[1] 
[u'/Users/massimo/Desktop/Twitter/recipe_get_friends_followers.py']

我的理解是,SCREEN_NAME应该包含我想从中提取关注者的twitterer的名称。并且MAX_IDS应该是我想要获得的最大关注者数量。

但是如何指定这样的参数呢?目前,他们不应该包含在argv列表中。

1 个答案:

答案 0 :(得分:1)

从终端

运行如下脚本:

python script.py some_user max_ids

当然,将some_user替换为所需的Twitter帐户,将max_ids替换为最大关注者数量