如何解压 tweepy 提供的 JSON
How to unpack JSON provided by tweepy
我使用基于this question的第一个答案的代码使用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
它提供了 json 个对象的列表,例如 searched_tweets[2]
输出(截断)
Status(_api=<tweepy.api.API object at 0x7fc13dbab828>, _json={'created_at': 'Wed Jun 10 14:06:51 +0000 2020', 'id': 1270719075388280834, 'id_str': '1270719075388280834', 'text': "RT @CDWGWAGov: According to @IBM's new CEO, #hybridcloud & #AI are the two dominant forces driving #digitaltransformation #Kubernetes #IoT…", 'truncated': False,
我需要创建日期和推文文本,所以我使用以下代码提取它们
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
我已尝试 this 解决此错误的方法 api = tweepy.API(auth, parser=tweepy.parsers.JSONParser())
它给出 KeyError: -1
我已经在 Whosebug 上尝试了几乎所有其他解决方案,但对我来说没有任何效果。有人可以帮我解压 json 并获得这两个值吗?谢谢
tweepy的Status
对象本身是不可JSON序列化的,但是它有一个_json
属性可以JSON序列化
例如
status_list = api.user_timeline(user_handler)
status = status_list[0]
json_str = json.dumps(status._json)
我怀疑错误是由这一行引起的
new_tweet = json.dumps(tweet)
在这里,所以只需调用 _json
属性 这一行
new_tweet = json.dumps(tweet._json)
并修改相关后续代码。这应该可以解决您的问题
我使用基于this question的第一个答案的代码使用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
它提供了 json 个对象的列表,例如 searched_tweets[2]
输出(截断)
Status(_api=<tweepy.api.API object at 0x7fc13dbab828>, _json={'created_at': 'Wed Jun 10 14:06:51 +0000 2020', 'id': 1270719075388280834, 'id_str': '1270719075388280834', 'text': "RT @CDWGWAGov: According to @IBM's new CEO, #hybridcloud & #AI are the two dominant forces driving #digitaltransformation #Kubernetes #IoT…", 'truncated': False,
我需要创建日期和推文文本,所以我使用以下代码提取它们
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
我已尝试 this 解决此错误的方法 api = tweepy.API(auth, parser=tweepy.parsers.JSONParser())
它给出 KeyError: -1
我已经在 Whosebug 上尝试了几乎所有其他解决方案,但对我来说没有任何效果。有人可以帮我解压 json 并获得这两个值吗?谢谢
tweepy的Status
对象本身是不可JSON序列化的,但是它有一个_json
属性可以JSON序列化
例如
status_list = api.user_timeline(user_handler)
status = status_list[0]
json_str = json.dumps(status._json)
我怀疑错误是由这一行引起的
new_tweet = json.dumps(tweet)
在这里,所以只需调用 _json
属性 这一行
new_tweet = json.dumps(tweet._json)
并修改相关后续代码。这应该可以解决您的问题