【问题标题】:Saving tweets using tweepy in json format使用 tweepy 以 json 格式保存推文
【发布时间】:2019-02-16 23:44:52
【问题描述】:

我们如何使用 tweepy 以 json 格式收集推文并保存到本地磁盘。我需要在 Solr 中输入 json 文件以进行索引和标记化。这是我正在使用的代码:

`

 import json
 import tweepy
 from tweepy import OAuthHandler

 ckey=""
 csecret=""
 atoken=""
 asecret=""

 auth = OAuthHandler(ckey, csecret)
 auth.set_access_token(atoken, asecret)

 api = tweepy.API(auth)

 data1 = api.search(q='politics', count = 1)`

【问题讨论】:

  • 您好,您能更具体地了解您看到的错误是什么吗?或者你得到的输出和你期望的结果是什么?
  • @toti08 .....我在打印 data1 时收到以下响应:{"created_at":"Sat Sep 08 17:44:57 +0000 2018","id":1038483340926509057 ,"id_str":"1038483340926509057","text":"RT : 喀拉拉邦洪水严重破坏了...} 我相信这只是一个 json 格式。我需要的是我需要在 python 中添加什么代码所以这个输出存储在我本地机器上的 json 文件中。

标签: python json rest tweepy information-retrieval


【解决方案1】:

您可以尝试将数据保存为list 并将其转储到json 文件中,前提是您已经导入了json。

z = [x for x in data1]

with open('your_data.json', 'w') as out:
    json.dump(z,out)

或者,您可以将z = [x for x in data1] 写为:

z = list()
for x in data1:
    z.append(x)

/ogs

【讨论】:

    猜你喜欢
    • 2015-06-17
    • 2014-06-25
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 2019-06-21
    • 2015-06-26
    • 2019-05-16
    相关资源
    最近更新 更多