Python – User object 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.

User

The User object in Tweepy module contains the information about a user.

Here are the list of attributes in the User object :

  • id : The ID of the user.
  • id_str : The ID of the user as a string.
  • name : The name of the user.
  • screen_name : The screen name of the user
  • location : The location of the user.
  • profile_location : The location of the the profile.
  • description : The description of the user account.
  • url : The URL of the user account.
  • entities : A dictionary containing URLs.
  • protected : Indicates if the user profile is protected or not.
  • followers_count : The number of followers the account has.
  • friends_count : The number of friends the account has.
  • listed_count : The number of public lists the account has been added to.
  • created_at : The time at which the account was created.
  • favourites_count : The number of statuses the account has favourited.
  • utc_offset : The UTC offset of the profile.
  • geo_enabled : Indicates whether the account is geo enabled or not.
  • verified : Indicates whether the user is verified or not.
  • statuses_count : The number of statuses updated by the user.
  • lang : The language of the account.
  • status : Fetches the latest status updated by the user.
  • contributors_enabled : Indicates whether the contributors are enabled or not.
  • is_translator : Indicates whether there is a translator or not.
  • is_translation_enabled : Indicates whether the translation is available or not.
  • profile_background_color : The background color of the profile.
  • profile_background_image_url : The URL of the background image of the profile.
  • profile_background_image_url_https : The secure URL of the background image of the profile.
  • profile_background_tile : The title of the background profile.
  • profile_image_url : The URL of the profile image.
  • profile_image_url_https : The secure URL of the profile image.
  • profile_banner_url : The URL of the profile banner.
  • profile_link_color : The color of the profile link.
  • profile_sidebar_border_color : The color of the profile sidebar border.
  • profile_sidebar_fill_color : The color of the profile sidebar.
  • profile_text_color : The color of the profile text.
  • profile_use_background_image : Indicates whether the profile is to use a background image or not.
  • has_extended_profile : Indicates whether the profile is extended or not.
  • default_profile : The default profile.
  • default_profile_image : The image of the default profile.
  • following : Indicates whether the authenticated user is following the user or not.
  • follow_request_sent : Indicates whether the authenticated user has requested to follow the user or not.
  • notifications: Indicates whether the authenticated user has turned on the notifications for the user or not.

Example : Consider the twitter account of GeeksforGeeks. Use get_user() method to fetch the user.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 screen name of the user
screen_name = "geeksforgeeks"
  
# fetching the user
user = api.get_user(screen_name)
  
# printing the information
print("The id is : " + str(user.id))
print("The id_str is : " + user.id_str)
print("The name is : " + user.name)
print("The screen_name is : " + user.screen_name)
print("The location is : " + str(user.location))
print("The profile_location is : " + str(user.profile_location))
print("The description is : " + user.description)
  
  
print("The url is : " + user.url)
print("The entities are : " + str(user.entities))
print("Is the account protected? : " + str(user.protected))
  
print("The followers_count is : " + str(user.followers_count))
print("The friends_count is : " + str(user.friends_count))
print("The listed_count is : " + str(user.listed_count))
print("The account was created on : " + str(user.created_at))
print("The favourites_count is : " + str(user.favourites_count))
print("The utc_offset is : " + str(user.utc_offset))
print("The geo_enabled is : " + str(user.geo_enabled))
print("The verified is : " + str(user.verified))
print("The statuses_count is : " + str(user.statuses_count))
print("The lang is : " + str(user.lang))
print("The status ID is : " + str(user.status.id))
print("The contributors_enabled is : " + str(user.contributors_enabled))
print("The is_translator is : " + str(user.is_translator))
print("The is_translation_enabled is : " + str(user.is_translation_enabled))
  
print("The profile_background_color is : " + user.profile_background_color)
print("The profile_background_image_url is : " + user.profile_background_image_url)
print("The profile_background_image_url_https is : " + user.profile_background_image_url_https)
print("The profile_background_tile is : " + str(user.profile_background_tile))
print("The profile_image_url is : " + user.profile_image_url)
print("The profile_image_url_https is : " + user.profile_image_url_https)
print("The profile_banner_url is : " + user.profile_banner_url)
print("The profile_link_color is : " + user.profile_link_color)
print("The profile_sidebar_border_color is : " + user.profile_sidebar_border_color)
print("The profile_sidebar_fill_color is : " + user.profile_sidebar_fill_color)
print("The profile_text_color is : " + user.profile_text_color)
print("The profile_use_background_image is : " + str(user.profile_use_background_image))
  
print("The has_extended_profile is : " + str(user.has_extended_profile))
print("The default_profile is : " + str(user.default_profile))
print("The default_profile_image is : " + str(user.default_profile_image))
print("Is the authenticated user following the account? : " + str(user.following))
  
print("Has the authenticated user requested to follow the account? : " + str(user.follow_request_sent))
print("Are notifications of the authenticated user turned on for the account? : " + str(user.notifications))

chevron_right


Output :



The id is : 57741058
The id_str is : 57741058
The name is : GeeksforGeeks
The screen_name is : geeksforgeeks
The location is : India
The profile_location is : None
The description is : Your one-stop destination to attain Coding Nirvana…
The url is : http://t.co/k2iUfxLRBj
The entities are : {‘url’: {‘urls’: [{‘url’: ‘http://t.co/k2iUfxLRBj’, ‘expanded_url’: ‘http://geeksforgeeks.org/’, ‘display_url’: ‘geeksforgeeks.org’, ‘indices’: [0, 22]}]}, ‘description’: {‘urls’: []}}
Is the account protected? : False
The followers_count is : 17706
The friends_count is : 11
The listed_count is : 142
The account was created on : 2009-07-17 20:02:09
The favourites_count is : 465
The utc_offset is : None
The geo_enabled is : False
The verified is : False
The statuses_count is : 13231
The lang is : None
The status ID is : 1271404442068422662
The contributors_enabled is : False
The is_translator is : False
The is_translation_enabled is : False
The profile_background_color is : FFFDF7
The profile_background_image_url is : http://abs.twimg.com/images/themes/theme13/bg.gif
The profile_background_image_url_https is : https://abs.twimg.com/images/themes/theme13/bg.gif
The profile_background_tile is : False
The profile_image_url is : http://pbs.twimg.com/profile_images/1138375574726955008/1fNUyEdv_normal.png
The profile_image_url_https is : https://pbs.twimg.com/profile_images/1138375574726955008/1fNUyEdv_normal.png
The profile_banner_url is : https://pbs.twimg.com/profile_banners/57741058/1591281311
The profile_link_color is : 119E39
The profile_sidebar_border_color is : D3D2CF
The profile_sidebar_fill_color is : E3E2DE
The profile_text_color is : 0D0C0C

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.