0
votes

I am following http://chimera.labs.oreilly.com/books/1234000001583/ch01.html to learn how to get information from tweets using Python (jupyter).

Under "Analyzing the 140 Characters", the author say's the following:

let's assume that we've extracted a single tweet from the search results and stored it in a variable named t.

He then continues, using this variable t to get more information out of that one tweet. But how do I extract a single tweet from the search results and store it in a variable named t?

I would like to do so to follow his code, i.e. using "t['text']" and what follows.

I can get the text using the following code:

search_results = twitter_api.search.tweets(q=q, count=count)
one_tweet = [status['text']
                for status in search_results['statuses'][0:1]]
print(one_tweet)`

What I, however, would like to have is more like

one_tweet = [status
                for status in search_results['statuses'][0:1]]
print(one_tweet['text'])
print(one_tweet['screen_name'])`

In other words, save the whole tweet and then specify what I would like to print

After

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

I get the following:

{{u'search_metadata': {u'count': 100, u'completed_in': 0.148, u'max_id_str': u'714086757604659200', u'since_id_str': u'0', u'refresh_url': u'?since_id=714086757604659200&q=%23dopa&include_entities=1', u'since_id': 0, u'query': u'%23dopa', u'max_id': 714086757604659200}, u'statuses': [{u'contributors': None, u'truncated': False, u'text': .....}
1

1 Answers

0
votes

When you have retrieved the tweet as in the snippet before in that source

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

the results are returned as a list and you can simply iterate over the search_results:

for t in search_results["statuses"]:
   print t['text']