Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate/parse Tweepy get_user object

Tags:

python

tweepy

In playing around with Tweepy I notice that the 'status' variable returned from a call to get_user is <tweepy.models.Status object at 0x02AAE050>

Sure, I can call get_user.USER.status, but how can I grab that information from the get_user call? i.e. I want to loop through user.getstate() and if I find an object which requires further iteration, loop through that too

I've searched high/low of answers, but my newness to Python is causing problems, which I'm pretty sure are easy to solve if I knew the right questions to ask.

Thanks for any pointer here...

# -*- coding: utf-8 -*-

import sys

import tweepy
import json

from pprint import pprint


api = tweepy.API()



def main():
    print "Starting."

    user = api.get_user('USER',include_entities=1)

    print "================ type ================="
    print type(user)

    print "================ dir ================="
    print dir(user)

    print "================ user ================="
    #
    # We can see 'status': <tweepy.models.Status object at 0x02AAE050>, .......but how do I "explode" that automagically?
    #
    pprint ((user).__getstate__())



    print "================ user.status ================="
    pprint ((user).status.__getstate__())

    print "================= end ================="





if __name__ == "__main__":
  main()

I was able to get the intended behaviour by using jsonpickle, using the following code.

import jsonpickle
.
.
.
user = api.get_user('USERNAME',include_entities=1)
    pickled = jsonpickle.encode(user)
    print(json.dumps(json.loads(pickled), indent=4, sort_keys=True))   #you could just  print pickled, but this makes it pretty

I'm still really interested to understand what I'm missing in understanding how to detect and expand that status object.

like image 233
RandomDude Avatar asked Sep 16 '25 10:09

RandomDude


1 Answers

try this:

api = tweepy.API(auth,parser=tweepy.parsers.JSONParser())

user = api.get_user('USER',include_entities=1)

now you can user user['status'] and others easily

like image 137
devesh varshney Avatar answered Sep 18 '25 08:09

devesh varshney