Python 将特定hashtag的tweet保存到JSON文件

Python 将特定hashtag的tweet保存到JSON文件,python,json,twitter,tweepy,twitterapi-python,Python,Json,Twitter,Tweepy,Twitterapi Python,我是编程新手,正在将python应用到一个需要分析特定Twitter标签的项目中。我已经成功地显示了特定hashtag的tweet,但是我无法将它们保存到JSON文件中 我将非常感谢您的帮助 下面是我的代码: #Import necessary libaries to access Twitter import os import tweepy as tw import pandas as pd import tweepy as OAuthHandler # Access keys and to

我是编程新手,正在将python应用到一个需要分析特定Twitter标签的项目中。我已经成功地显示了特定hashtag的tweet,但是我无法将它们保存到JSON文件中

我将非常感谢您的帮助

下面是我的代码:

#Import necessary libaries to access Twitter
import os
import tweepy as tw
import pandas as pd
import tweepy as OAuthHandler
# Access keys and tokens to Twitter 
consumer_key=''
consumer_secret=''
access_token=''
access_token_secret=''
#Access Twitter
auth = tw.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
#this to allow us to generate multiple requests and skip the waiting time
api = tw.API(auth, wait_on_rate_limit=True)
 #Define the query of our search
#query='#Astrazeneca'
query=input('print selected hashtag starting with #: ')
tweets=tw.Cursor(api.search,q=query,lang='en').items(50)
#for tweet in tweets:
    #print(tweet.text)
# make the tweets in a list
all_tweets=[tweet.text for tweet in tweets]
all_tweets[0:5]
我收到的错误是:

AttributeError                            Traceback (most recent call last)
<ipython-input-4-a82813c9d641> in <module>
      3 for tweet in all_tweets:
      4     with open('tweet.text_json','w',encoding='utf8') as file:
----> 5         json.dump(tweet._json, file,sort_keys = True,indent = 4)

AttributeError: 'str' object has no attribute '_json'
AttributeError回溯(最近一次调用)
在里面
3对于所有_推文中的推文:
4以open('tweet.text_json','w',encoding='utf8')作为文件:
---->5 json.dump(tweet.\u json,file,sort\u keys=True,indent=4)
AttributeError:'str'对象没有属性'\u json'

当您创建列表时
所有tweet
您正在调用
tweet.text
,它返回一个
字符串。
string
没有属性
\u json
,但是您的
tweet
对象(在调用
.text
之前)有属性

将其更改为包含对象
tweet
,这将打开循环中调用
tweet.\u json
的功能

all_tweets=[tweet for tweet in tweets]
all_tweets=[tweet for tweet in tweets]