Convert Tweepy Status object into JSON

 ̄綄美尐妖づ 提交于 2019-11-27 06:53:35

The Status object of tweepy itself is not JSON serializable, but it has a _json property which contains JSON serializable response data. For example:

>>> status_list = api.user_timeline(user_handler)
>>> status = status_list[0]
>>> json_str = json.dumps(status._json)

A better way to do this is to use a tweepy parser. It's not documented very well - see the Tweepy API reference - but it's a public API, so much safer than using the _json property.

import tweepy
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth, parser=tweepy.parsers.JSONParser())
status = api.user_timeline(user=username, count=1)[0]
json.dumps(status)

status is now a json object.

users = api.search_users('TimHortons', 1)
print(json.dumps(users[0]._json))

Use json.dumps(users[0]._json) if object has _json. Users was only an example.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!