#python #json #twitter #save #twitterapi-python
Вопрос:
Я пытаюсь сохранить твиты с определенным хэштегом в файл JSON, код не выдает мне никаких ошибок. однако данные не сохраняются, и я не могу найти файл на своем устройстве.
У меня есть еще один вопрос, как я могу увеличить количество твитов, сохраненных за 2 дня ?
ищу вашу поддержку, спасибо
# import tweepy library to access Twitter REST APIs service
import tweepy
#Python code to get twitter access
#Authorize access to Twitter using access keys amp; 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
Комментарии:
1. вы не вызвали функцию
2. Кроме того, в строке
json.dump(tweet._json,file,sort_keys = True,indent = 4)
вы можете попробовать заменитьfile
наoutfile
.3. Спасибо за ваш ответ :), я попытался заменить файл на outfile, но это не сработало. Я все еще в начале процесса обучения, поэтому не могли бы вы, пожалуйста, рассказать мне, как написать код для вызова функции? Огромное спасибо