Reputation: 99
I'm trying to create strings from the u'text':
& u'id':
found in u'results':
So that I can compare new IDs to the last one read and display the text if it is new. I cannot currently parse(?) only the information I want.
import json
import urllib
from pprint import pprint
j =json.loads(urllib.urlopen('http://search.twitter.com/search.json?q=%23tory&result_type=recent&rpp=1').read())
pprint(j)
This gives me
{u'completed_in': 0.007,
u'max_id': 312184292639920129L,
u'max_id_str': u'312184292639920129',
u'next_page': u'?page=2&max_id=312184292639920129&q=%23tory&rpp=1&result_type=recent',
u'page': 1,
u'query': u'%23tory',
u'refresh_url': u'?since_id=312184292639920129&q=%23tory&result_type=recent',
u'results': [{u'created_at': u'Thu, 14 Mar 2013 12:51:50 +0000',
u'from_user': u'DerbysLabour',
u'from_user_id': 393219652,
u'from_user_id_str': u'393219652',
u'from_user_name': u'Derbyshire Labour',
u'geo': None,
u'id': 312184292639920129L,
u'id_str': u'312184292639920129',
u'iso_language_code': u'en',
u'metadata': {u'result_type': u'recent'},
u'profile_image_url': u'http://a0.twimg.com/profile_images/1594091282/Labour_group_normal.jpg',
u'profile_image_url_https': u'https://si0.twimg.com/profile_images/1594091282/Labour_group_normal.jpg',
u'source': u'<a href="http://twitter.com/">web</a>',
u'text': u'RT @WubeyOneKenobi: Not seen a single #Tory campaigning for #DCC elections round #Glossop yet. Scared to come out?'}],
u'results_per_page': 1,
u'since_id': 0,
u'since_id_str': u'0'}
in return (depending on the tweet)
How do we read the Text & ID from the Results only?
Upvotes: 0
Views: 151
Reputation: 761
The j
variable is a dict
, and results
is an array containing a dict
. You can extract the values like so:
text = j['results'][0]['text']
id = j['results'][0]['id']
If you have more results, you can do the following:
texts = {}
results = j['results']
for result in results:
text = result['text']
id = results['id']
texts[id] = text
As a result you have a single dict
called texts
where the id is the key.
Upvotes: 2