import tweepy import json consumer_key = "r8y13uUfAduFvj8X4dsEdAMWG" consumer_secret = "gBAaLFgxTXXnLlnqAmPJKpD22Cg3L0I6tW9yy0kdY34pv8cEZO" access_token = "1150328765597310976-Sww8Yua53EmGOnkr09cPkUylS1hSuO" access_token_secret = "OIIurB22pKTax4FYbs9QNRuXTLFhasSLfqI0Zd4lkjkm4" #Now it’s time to create our API object. # Creating the authentication object auth = tweepy.OAuthHandler(consumer_key, consumer_secret) # Setting your access token and secret auth.set_access_token(access_token, access_token_secret) # Creating the API object while passing in auth information api = tweepy.API(auth) # The Twitter user who we want to get tweets from name = "nytimes" # Number of tweets to pull tweetCount = 20 # Calling the user_timeline function with our parameters #results = api.user_timeline(id=name, count=tweetCount) result= [] #//OPTION 1 : WITH JSON ELEMENTS #with open('tweet.json', 'a', encoding='utf8') as file: # foreach through all tweets pulled # for tweet in results: # printing the text stored inside the tweet object # print (tweet.text) # result.append({ # 'text': tweet.text, # 'author_name': tweet.user.screen_name # }) # json.dump(tweet._json,file,sort_keys = True,indent = 4) #The search term you want to find query = "@EPS_UdL" # Language code (follows ISO 639-1 standards) #language = "en" # Calling the user_timeline function with our parameters results = api.search(q=query, count= 1000000) # foreach through all tweets pulled for tweet in results: # printing the text stored inside the tweet object print(tweet.text) result.append({ 'text': tweet.text, 'author_name': tweet.user.screen_name, #'created': tweet.created_at, 'location': tweet.user.location }) #//OPTION 2 : WITH LIST OF TWEETS result with open('tweet.json', 'w', encoding='utf8') as file: json.dump(result, file, sort_keys=True, indent=4)