0

我正在尝试从 Twitter API 中提取推文并将它们转储为 JSON 格式。

for tweet in tweepy.Cursor(api.search,q="kmart",count=200,lang="en",tweet_mode='extended').items(1000):
    print(tweet.full_text)
    with open('tweets.json', 'a') as f:
            f.write(json.dumps(tweet._json))

这工作得很好,但在 tweets.json 文件中只有一行包含所有推文。如何将每条推文放在单独的行中?

此外,在某些推文中,我仍然没有得到整个推文。为什么会这样?

4

1 回答 1

0

您可以将值indent作为参数传递给json.dumps方法

for tweet in tweepy.Cursor(api.search,q="kmart",count=200,lang="en",tweet_mode='extended').items(1000):
print(tweet.full_text)
with open('tweets.json', 'a') as f:
        f.write(json.dumps(tweet._json, indent=4))
于 2019-08-22T03:00:29.610 回答