Search code examples
pythontwittertwitterapi-python

How to get the data in python-twitter


I'm new to python. I am using the libray called python-twitter to grab data of my own twitter account

Here is my code (twitter-test.py)

import twitter

api = twitter.Api(consumer_key="xxxxxxxx",
                  consumer_secret="xxxxxxxxxx",
                  access_token_key="xxxxxx-xxxx",
                  access_token_secret="xxxxxxx",
                  sleep_on_rate_limit=True)
follower = api.GetFollowers()
print(follower)

then i run python3 twitter-test.py Here is the result.

[User(ID=xxxxxxxxx, ScreenName=xxxxxxx), User(ID=xxxxxxxxx, ScreenName=xxxxxxx), User(ID=xxxxxxxxx, ScreenName=xxxxxxx), .......]

The api is call successfully.

Now I want to save the result:

import json

{ The code same as before }

json.dumps(follower)
TypeError: Object of type User is not JSON serializable

Then I tried to loop the result using for x in follower:

but x[0] x['ID'] x.ID all return error:

TypeError: 'User' object is not subscriptable

How can I extract the data from User Object ?


Solution

  • this is works fine for me

    GetFollowers() function returns an user object in a list you can find object details here

    followers = api.GetFollowers()
    for follower in followers:
        print(follower.created_at)
        print(follower.name)