ptwobrussell / Mining-the-Social-Web

The official online compendium for Mining the Social Web (O'Reilly, 2011)
http://bit.ly/135dHfs
Other
1.21k stars 490 forks source link

the_tweet__search with geocode #58

Closed vivdx closed 11 years ago

vivdx commented 11 years ago

import sys import twitter from twitter_login import login

q = "xxxxx" count = 500 geocode = 'xx.055207,xx.745055,10mi' MAX_PAGES = 5

t = login()

search_results = t.search.tweets(q=q, geocode=geocode, count=count) tweets = search_results['statuses']

for _ in range(MAX_PAGES-1): next_results = search_results['search_metadata']['next_results'] kwargs = dict([ kv.split('=') for kv in next_results[1:].split("&") ]) # Create a dictionary from the query string params search_results = twitter_api.search.tweets(**kwargs) tweets += search_results['statuses'] if len(search_results['statuses']) == 0: break

print 'Fetched %i tweets so far' % (len(tweets),)

import json print json.dumps(statuses[0:1], indent=1)

tweets = [ status['text'] for status in statuses ]

print tweets[0]

ERROR: next_results = search_results['search_metadata']['next_results'] KeyError: 'next_results'

ptwobrussell commented 11 years ago

Did you try debugging this at all? Here's what I tried...

import sys
import twitter
from twitter__login import login

q = "the"
count = 500
geocode = '31.055207,21.745055,10mi'
MAX_PAGES = 5

t = login()

search_results = t.search.tweets(q=q, geocode=geocode, count=count)

# See what you've got so far...
print >> sys.stderr, search_results

tweets = search_results['statuses']

assert len(tweets) > 0, "No need to continue. There were no tweets for this query"

for _ in range(MAX_PAGES-1): 
    next_results = search_results['search_metadata']['next_results']
    kwargs = dict([ kv.split('=') for kv in next_results[1:].split("&") ]) # Create a dictionary from the query string params
    search_results = twitter_api.search.tweets(**kwargs)
    tweets += search_results['statuses']
    if len(search_results['statuses']) == 0:
        break

What I discovered (and think that is your problem) is that there just aren't any tweets from the query you are running, so the loop immediately has an error on it. The sys.stderr will show you that much for sure, and the assert statement is one way you could stop the program if that condition is true. Another would be to use conditional logic and do an exit as a common example.

I'll defer to you on closing this ticket, but I think that's all that's happening. Close it if you can confirm and agree? Thank you!

vivdx commented 11 years ago

thanks a lot for such a quick reply.

print >> sys.stderr, search_results

works and prints some results.

But still there is the same Error : KeyError : 'next_results'

I dont know why but there is some problem with for loop

vivdx commented 11 years ago

It worked. You were right.