我使用基于这个问题的第一个答案的代码使用 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
我已经尝试过这个解决方案来解决这个错误,api = tweepy.API(auth, parser=tweepy.parsers.JSONParser())我KeyError: -1 已经在 stackoverflow 上尝试了几乎所有其他解决方案,但对我没有任何帮助。有人可以帮我解压 json 并获得这两个值吗?谢谢
交互式爱情
相关分类