How to access the structure of this Twitter result?

4

I have this portion of code: use (Tweepy)

firstTweet = api.user_timeline('twitter')[0]
print(firstTweet.text)
print(firstTweet.id)
results = api.retweets(firstTweet.id)
print(results)

When I get the result I get the following:

  

[Status (possibly_sensitive = False, in_reply_to_user_id = None, contributors = None, retweeted = False, retweet_count = 77, favorite_count = 0, created_at = datetime.datetime (2016, 2, 2, 12, 56, 25), text = 'RT @twitter: Follow conversation about #iCaucused live as the results come in: link link ', place = None, lang =' en ', entities = {' symbols ': [],' urls ': [{' display_url ':' twitter.com/search?q =% 23iC ... ',' expanded_url ':' link ',' url ':' link ',' index ': [79, 102]}, {' display_url ':' twitter.com/gov/status/694... ',' expanded_url ':' link ',' url ':' link ',' indices': [103, 126]}], 'user_mentions': [{' id_str ':' 783214 ',' screen_name ':' twitter ',' id ': 783214, 'name': 'Twitter', 'index': [3, 11]}], 'hashtags': [ {'text': 'iCaucused', 'indices': [39, 49]}]}, user = User (profile_image_url =' link ', is_translator = False, profile_banner_url =' link ', description =' ' , contributors_enabled = False, screen_name = 'abdulazeeznima2', profile_image_url_https = ' link .....

Short for space reasons ...

I am getting information on who has done RT (or who) .... now how do I access each of the items?

Example: Who gave RT? What is your screen_name ? Do you have GEO activated?

Thanks to everyone!

Another question ... why is that json (if it is) malformed and where and how can I format it correctly to display it in an orderly manner?

    
asked by papabomay 02.02.2016 в 14:56
source

1 answer

3

The results you get are lists of objects Status , which is nothing more than wrappers of the data returned by the twitter API. Among the attributes of the object Status you have the information you are looking for:

retweets = api.retweets(firstTweet.id)
users = [ tw.user for tw in retweets ]
user_names = [ user.screen_name for user in users ]

The attributes of each object depend solely on the twitter API ; tweepy only does an encapsulation of the results to facilitate programming in python. Use the twitter API directly if you want to process the answers in json .

And, as usual in python, to obtain the attributes of an object the dir() function is used.

EDITED: The typical python introspection mechanism is using the dir() function and navigating through the attributes of an object. Tweepy provides all its models with the .__getstate__() method to get the attribute dictionary that is possibly what you want.

>>> status.__getstate__() 
{
 'contributors': None, 
 'truncated': False, 
 'text': '....',
 'in_reply_to_status_id': None,
 'id': 21041793667694593,
 '_api': <tweepy.api.api object="" at="" 0x6bebc50="">,
 'author': <tweepy.models.user object="" at="" 0x6c16610="">,
...

>>> status.author.__getstate__()
{
....

Anyway, I recommend you take a look at the model source code so you can clarify how it works.

All models derive from the class Model , which has a parse method to process the json responses. This method converts each json attribute into attributes of the model:

class Status(Model):

    @classmethod
    def parse(cls, api, json):
        status = cls(api)
        setattr(status, '_json', json)
        for k, v in json.items():
            if k == 'user':
                user_model = getattr(api.parser.model_factory, 'user') if api else User
                user = user_model.parse(api, v)
                setattr(status, 'author', user)
                setattr(status, 'user', user)  # DEPRECIATED
            elif k == 'created_at':
                setattr(status, k, parse_datetime(v))
            elif k == 'source':
                if '<' in v:
                    setattr(status, k, parse_html_value(v))
                    setattr(status, 'source_url', parse_a_href(v))
                else:
                    setattr(status, k, v)
                    setattr(status, 'source_url', None)
            elif k == 'retweeted_status':
                setattr(status, k, Status.parse(api, v))
            elif k == 'place':
                if v is not None:
                    setattr(status, k, Place.parse(api, v))
                else:
                    setattr(status, k, None)
            else:
                setattr(status, k, v)
        return status

As you can see, the first thing you do is enter the answer json in the attribute _json , which would answer the question you asked about it. Then there are some attributes that have a specific processing: author/ user , created_at , source , retweeted_status and place . The rest of attributes are put directly as attributes of the model. The remaining models are similar. From here on, I hope it's easier for you to work with this API.

    
answered by 02.02.2016 / 17:22
source