Open In App

Python – API.create_saved_search() in Tweepy

Improve
Improve
Like Article
Like
Save
Share
Report

Twitter is a popular social network where users share messages called tweets. Twitter allows us to mine the data of any user using Twitter API or Tweepy. The data will be tweets extracted from the user. The first thing to do is get the consumer key, consumer secret, access key and access secret from twitter developer available easily for each user. These keys will help the API for authentication.

API.create_saved_search()

The create_saved_search() method of the API class in Tweepy module is used to create a saved search for the authenticated user.

Syntax : API.create_saved_search(query)

Parameter :

  • query : the query of the search.

Returns : an object of class SavedSearch

Example 1 : Creating a saved search query.




# import the module
import tweepy
  
# assign the values accordingly
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
  
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  
# set access to user's access key and access secret 
auth.set_access_token(access_token, access_token_secret)
  
# calling the api 
api = tweepy.API(auth)
  
print("The number of saved searches before create_saved_search() : ", end = "")
print(len(api.saved_searches()))
  
# query of the new saved search
query = "Tweepy"
  
# creating the saved search
api.create_saved_search(query)
  
print("The number of saved searches after create_saved_search() : ", end = "")
print(len(api.saved_searches()))


Output :

The number of saved searches before create_saved_search() : 3
The number of saved searches after create_saved_search() : 4

Example 2 : Getting the newly created saved search.




# query of the new saved search
query = "News"
  
# creating the saved search
saved_search = api.create_saved_search(query)
  
# printing the information
print("The saved search " + str(saved_search.id) +
      " was created on : " + str(saved_search.created_at))
print("The query of the saved search is : " + saved_search.query)


Output :

The saved search 1269510986496569349 was created on : 2020-06-07 06:06:20
The query of the saved search is : News


Last Updated : 10 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads