Python – API.search_users() in Tweepy
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.search_users()
The search_users() method of the API class in Tweepy module is used to run a search based on a query and get users matching that query, similar to the Find People button.
Syntax : API.search_users(q, count)
Parameters :
q : the text to be searched
count : specifies the number of users to retrieve, cannot be greater than 20, the default value is 20Returns : a list of objects of the class User
Example 1 :By default search_users() retrieves 20 users.
# import the moduleimport tweepy # assign the values accordinglyconsumer_key = ""consumer_secret = ""access_token = ""access_token_secret = "" # authorization of consumer key and consumer secretauth = 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) # the query to be searchedq = "geeks" # search the queryusers = api.search_users(q) # print the users retrievedfor user in users: print(user.screen_name) |
Output :
GeeksOUT GWOBorg GeeksOfColor geeksaresexy WeatherGeeks CoachingGeeks DealsForGeeks TwinCitiesGeeks RetroGameGeeks BuddhistGeeks KevinGeeksOut mcrgirlgeeks AirplaneGeeks AirlineGeeks geeks_3d GeeksGamersCom javacodegeeks wearemoviegeeks The_GWW GeeksRoom
Example 2: Using the count parameter to retrieve less than 20 users.
# the query to be searchedq = "geeksforgeeks" # number of users to be retrievedcount = 3 # search the queryusers = api.search_users(q, count) # print the users retrievedfor user in users: print(user.screen_name) |
Output :
geeksforgeeks gfgvideos gfg_reader
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


