问题描述
我正在尝试将带有特定主题标签的推文保存到 JSON 文件中,代码没有给我任何错误。但是,数据没有保存,我在我的设备上找不到文件。
寻求您的支持, 谢谢
# import tweepy library to access Twitter REST APIs service
import tweepy
#Python code to get twitter access
#Authorize access to Twitter using access keys & tokens from my developer account using tweepy OAuthHandler method
from tweepy import OAuthHandler
import tweepy as tw
consumer_key=''
consumer_secret=''
access_token=''
access_token_secret=''
#online authuntication
auth = tweepy.OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_token_secret)
api = tweepy.API(auth) #The api variable is Now our entry point for most of the operations we can perform with Twitter.
#import JSON
import json
query='#vaccine' # define the query of our search
def process_or_store(tweet): #define process or store function to save the tweets with #vaccine to JSON local file
#open local file names (vaccinetweets)
with open('vaccinetweets.json','w') as outfile:
#write the tweets to the saved file and make it in beatiful print mode
json.dump(tweet._json,file,sort_keys = True,indent = 4)
# mt.write(json.dumps(tweet)+'\n') #write the tweets to the saved file and insert a line between the tweets
#with the for loop use the cursor interface to navigate through tweets meeting our search criteria
for tweet in tweepy.Cursor(api.search,q=query,lang="en",since='2021-04-01').items():
process_or_store(tweet._json) #save the tweets
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)