Greetings fellow enthusiasts:
I am debugging a program integrated with the twitter-api where the purpose is to search for specific tweets in a given radius and returns them in a csv file.
It works….yet sometimes it encounters a ” KeyError: ‘location’ ” error yet there is no key error when I check it so can anyone shed any light as to why it’s returning this error?
If you uncomment out the pprint statement to see the raw returned data from twitter-api it is clear ‘location’ is the correct key and works sometimes but sometimes not and returns the aforementioned error.
Below is the code run it with the test input given in the comments:
import urllib2, json, pprint, codecs, unicodedata, csv
## test coordinate input: 29.762778,-95.383056
## test radius 10
## test query: tebow
##Initial user input
city = raw_input("Please enter to 6 decimal places the city\ncoordinates to be searched ex. lat,long: ")
radius = raw_input("Please enter the numeric value of the\nradius in miles you'd like to search ex. 10: ")
term= raw_input("Please enter the search term you wish to query ex. tebow: ")
u = urllib2.urlopen('http://search.twitter.com/search.json?q='+term+'&geocode='+city+','+radius+'mi&page=1&rpp=20')
datares = json.load(u)
##pprint.pprint(datares)
with codecs.open('Gtweets.csv',mode='w', encoding='ascii',errors='ignore') as cache:
writer = csv.writer(cache)
for tweet in datares['results']:
writer.writerow([tweet['text'].encode('ascii', 'ignore'), tweet['location'].encode('ascii', 'ignore'), tweet['created_at'].encode('ascii', 'ignore'), tweet['from_user'].encode('ascii', 'ignore')])
Since you don’t have any control over whether Twitter includes the location or not, don’t rely on the field being present. Instead of using square brackets for the lookup, use dict.get instead: