4

tweepyを使用して最新のユーザーステータスを取得しようとしています

私のコードは

api = tweepy.API(auth)
for status in tweepy.Cursor(api.user_timeline).items():
    lastid = status.id
    laststatus = api.get_status(lastid).text
    break

できます。しかし、私はループを使用する必要があります。より良い方法はありますか?

4

1 に答える 1

5

.items()イテレータを返すので、呼び出すだけnext()で最初のアイテムを取得できます。

status = next(tweepy.Cursor(api.user_timeline).items())

アイテムがまったくない場合、これにより例外が発生する可能性があります。これを防ぐためにStopIteration、デフォルトを追加できます。next()

status = next(tweepy.Cursor(api.user_timeline).items(), None)
于 2013-03-15T16:12:18.880 に答える