Python - User object in Tweepy

Python - User object in Tweepy

Tweepy is a Python library that provides a convenient way to access the Twitter API. The User object in Tweepy represents a Twitter user and provides various attributes related to that user.

Here's how you can use the User object in Tweepy:

  • First, install tweepy if you haven't:
pip install tweepy 
  • Set up Tweepy with your Twitter API credentials:
import tweepy consumer_key = 'YOUR_CONSUMER_KEY' consumer_secret = 'YOUR_CONSUMER_SECRET' access_token = 'YOUR_ACCESS_TOKEN' access_token_secret = 'YOUR_ACCESS_TOKEN_SECRET' auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) 

Replace the placeholders with your actual API credentials.

  • Fetch a User object and explore its attributes:
# Fetch a user by screen_name user = api.get_user(screen_name='twitter_username') # Print some information about the user print(f"User ID: {user.id}") print(f"Name: {user.name}") print(f"Screen Name: {user.screen_name}") print(f"Description: {user.description}") print(f"Location: {user.location}") print(f"Followers Count: {user.followers_count}") print(f"Friends Count: {user.friends_count}") print(f"Created At: {user.created_at}") print(f"Profile Image URL: {user.profile_image_url}") 

Replace 'twitter_username' with the desired Twitter handle.

The User object contains many more attributes; the ones listed above are just a few examples. You can explore the official Tweepy documentation or use Python's dir() function to see all the attributes and methods available for a User object:

print(dir(user)) 

Note: Remember to always respect Twitter's rate limits when making requests, and consider catching potential exceptions (like tweepy.RateLimitError) that may be raised by the Tweepy API methods.


More Tags

oauth doctrine-odm correlation linker-scripts signature anchor ssim maven-3 extension-methods ruby-on-rails-3.2

More Programming Guides

Other Guides

More Programming Examples