Search code examples
pythontwitter

Searching Tweets in Python


Have a look at the following code:

q = '#MentionSomeoneImportantForYou'

count = 100

search_results = twitter_api.search.tweets(q=q, count=count)

#twitter_api is predefined and is working fine.

statuses = search_results['statuses']

for _ in range(5):
   print "Length of statuses", len(statuses)
   try:
       next_results = search_results['search_metadata']['next_results']
   except KeyError, e: # No more results when next_results doesn't exist
       break

kwargs = dict([ kv.split('=') for kv in next_results[1:].split("&") ])

The last code throws an error that 'next_results' is not defined.

Where did I go wrong on this?


Solution

  • I really don't get why you have to

    next_results = search_results['search_metadata']['next_results']
    

    while this line will return the same result in 5 times ?

    Anw, "next_results" is not defined means that the line above hasn't been reached even 1 time.

    How about

    print search_results['search_metadata']
    

    to see exactly how was the API's response ?