Python 如何解包tweepy提供的JSON

Python 如何解包tweepy提供的JSON,python,json,twitter,tweepy,Python,Json,Twitter,Tweepy,我使用基于的第一个答案的代码,使用tweepy抓取推文,如下所示 consumer_key = "" consumer_secret = "" access_token = "" access_token_secret = "" import tweepy auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api

我使用基于的第一个答案的代码,使用tweepy抓取推文,如下所示

consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""

import tweepy

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True)

query = 'kubernetes'
max_tweets = 200
searched_tweets = []
last_id = -1
while len(searched_tweets) < max_tweets:
    count = max_tweets - len(searched_tweets)
    try:
        new_tweets = api.search(q=query, count=count, max_id=str(last_id - 1))
        if not new_tweets:
            break
        searched_tweets.extend(new_tweets)
        last_id = new_tweets[-1].id
    except tweepy.TweepError as e:
        break
我需要创建日期和tweet文本,所以我使用以下代码来提取它们

for tweet in searched_tweets:
  new_tweet = json.dumps(tweet)
  dct = json.loads(new_tweet._json)
  created_at=dct['created_at']
  txt=dct['text']
但这是给予

TypeError: Object of type 'Status' is not JSON serializable
我已尝试解决此错误的解决方案,它是
api=tweepy.api(auth,parser=tweepy.parsers.JSONParser())
it give
KeyError:-1

我尝试过stackoverflow上的几乎所有其他解决方案,但都不管用。有人能帮我解压json并得到这两个值吗?谢谢

tweepy的
状态
对象本身不是JSON可序列化的,但它有一个
\u JSON
属性可以JSON序列化

比如说

status_list = api.user_timeline(user_handler)
status = status_list[0]
json_str = json.dumps(status._json)
我怀疑错误是由这条线引起的
new\u tweet=json.dumps(tweet)
在这里,因此只需调用此行的
\u json
属性即可

new_tweet = json.dumps(tweet._json)
并修改相关的后续代码。这应该能解决你的问题

new_tweet = json.dumps(tweet._json)