1

I can use the python Json library to extract information from this address

http://search.twitter.com/search.json?q=%23damn&result_type=recent&rpp=1&filter:retweets

and most of the time using

j =json.loads(urllib.urlopen('http://search.twitter.com/search.json?q=%23damn&result_type=recent&rpp=1&filter:retweets').read())

text = j['results'][0]['text']
id = j['results'][0]['id']

I can extract the text and ID from the results and pprint them. I am requesting JSON every 15 seconds so I don't get blocked by twitter's limit.

Every so often I encounter this.

   {u'completed_in': 0.021,
 u'max_id': 313306991827238912L,
 u'max_id_str': u'313306991827238912',
 u'next_page': u'?page=2&max_id=313306991827238912&q=%23damn&rpp=1&result_type=recent',
 u'page': 1,
 u'query': u'%23damn',
 u'refresh_url': u'?since_id=313306991827238912&q=%23damn&result_type=recent',
 u'results': [],
 u'results_per_page': 1,
 u'since_id': 0,
 u'since_id_str': u'0'}

Nothing appears to be contained inside of the results field. This causes the following error.

  Traceback (most recent call last):
  File "C:\Users\Home\Desktop\test.py", line 32, in <module>
    text = j['results'][0]['text']
IndexError: list index out of range

Which in turn closes the Python Command Line. I have considered placing a 'while' loop to make sure the results field is full before proceeding but I feel this could send to many requests and get the script locked out of twitter.

Have you encountered this problem? Do you know how to overcome it?

Hooperstu
  • 99
  • 1
  • 11

1 Answers1

0

I would either throw in an if statement before calling the results or a try, I've noticed in my time using web API's that the results aren't always reliable, better to be on the safe side

Matt Camilli
  • 664
  • 5
  • 8