-2
"21 recipes for mining Twitter"という本から1つのレシピを複製しています。 コードをコピーして貼り付けたにもかかわらず、わからないエラーが発生します。 SCREEN_NAME上エラーを理解できません:リストインデックスが範囲外です
list index out of range
:
は、ここで私はそれを実行すると、私が取得コード
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
になります。
sys.argv[1]
[u'/Users/massimo/Desktop/Twitter/recipe_get_friends_followers.py']
私の理解では、SCREEN_NAMEは私から信者を抽出したいtwittererの名前が含まれているべきであるということです。事実で
は、sys.argvのは、スクリプト名に対応するだけで1つのアイテムを持っています。 MAX_IDSは取得したいフォロワーの最大数にする必要があります。
しかし、このようなパラメータを指定するにはどうすればよいですか?現時点では、彼らはargvリストに含まれていません。端末から
どのようにこのスクリプトを実行している最大数と
max_ids
? –スクリプトを端末から 'python script.py%screen_name%%max_ids%' –
として実行する必要があります。 'sys.argv'にはスクリプト名である項目が1つしかない場合、' 1'_要素はありません。したがって、 'sys.argv [1]'がスクリプト名であることを示すサンプル出力が作成されます。これは誰でも問題のトラブルシューティングや助けに役立ちません。 – TigerhawkT3