简体   繁体   中英

python twython tweet extraction

I am using twython to connect to Twitter API and retrieve a query. Here is an example of what I get:

---START----

{u'next_page': u'?page=2&max_id=215397707'
....
: 215397707794219008L, u'page': 1}

----END----

What I am interested in is the actual tweet which starts like this:

u'text': u'@ilariargn devi fare un corso sul respiro! Sarebbero 7686879 euro, ci vieni? #perderetempo', u'from_user_name':

So everything between "u'text': u'" and "u'from_user_name'". I tried to get this information first with key and value because it is a dictionary. Does not work. Then I tried to convert the dictionary into a list and use a string search to get the position. Does not work either. How do I get this information?

I tried suggested solutions and received following messages:

tweet = json.loads(results)[u'text']
File "C:\Python27\lib\json\__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer

And for the second solution - if I write [t'text] I get a syntax error:

tweet = results[u'text']
KeyError: u'text'

Ok. After some trials I found the following solution to access the actual tweets:

counter = 1
for key, value in results.iteritems():
    if key == "results":
        svalue = str(value)
        while counter != -1:
            tweetposstart = svalue.find("u'text'",counter)
            tweetposend = svalue.find("u'from_user_name'",counter)
            if tweetposstart != -1:
                tweet = svalue[tweetposstart:tweetposend]
                print tweet
                counter = counter + tweetposstart
            else:
                counter = tweetposstart

What you get is a json object:

import json
tweet = json.loads(whatyouget)[u'text']

Or, if you get the dic already parsed, then:

tweet = whatyouget[t'text']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM