Open In App

Python – API.search_users() in Tweepy

Last Updated : 05 Jun, 2020
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.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 20

Returns : a list of objects of the class User

Example 1 :By default search_users() retrieves 20 users.




# 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)
  
# the query to be searched
q = "geeks"
  
# search the query
users = api.search_users(q)
  
# print the users retrieved
for 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 searched
q = "geeksforgeeks"
  
# number of users to be retrieved
count = 3
  
# search the query
users = api.search_users(q, count)
  
# print the users retrieved
for user in users:
    print(user.screen_name)


Output :

geeksforgeeks
gfgvideos
gfg_reader


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads