On Sat, Nov 10, 2012 at 3:58 PM, Roy Smith <r...@panix.com> wrote: > I'm trying to pull down tweets with one of the many twitter APIs. The > particular one I'm using (python-twitter), has a call: > > data = api.GetSearch(term="foo", page=page) > > The way it works, you start with page=1. It returns a list of tweets. > If the list is empty, there are no more tweets. If the list is not > empty, you can try to get more tweets by asking for page=2, page=3, etc. > I've got: > > page = 1 > while 1: > r = api.GetSearch(term="foo", page=page) > if not r: > break > for tweet in r: > process(tweet) > page += 1 > > It works, but it seems excessively fidgety. Is there some cleaner way > to refactor this?
I'd do something like this: def get_tweets(term): for page in itertools.count(1): r = api.GetSearch(term, page) if not r: break for tweet in r: yield tweet for tweet in get_tweets("foo"): process(tweet) -- http://mail.python.org/mailman/listinfo/python-list