Getting user information
There are multiple ways to retrieve user information in Twikit.By screen name
By user ID
Get authenticated user info
User attributes
TheUser object provides comprehensive information:
Learn more about Mintlify
Enter your email to receive updates about new features and product releases.
Learn how to interact with users, manage follows, and retrieve user information
user = await client.get_user_by_screen_name('elonmusk')
print(f'Name: {user.name}')
print(f'ID: {user.id}')
print(f'Followers: {user.followers_count}')
user = await client.get_user_by_id('44196397')
print(f'Screen name: {user.screen_name}')
# Get your own user information
me = await client.user()
print(f'Logged in as: {me.screen_name}')
print(f'Your user ID: {me.id}')
# Or just get your user ID
my_id = await client.user_id()
User object provides comprehensive information:
user = await client.get_user_by_screen_name('example_user')
# Basic information
print(user.id) # User ID
print(user.screen_name) # @username
print(user.name) # Display name
print(user.description) # Bio
print(user.location) # Location string
print(user.url) # Profile URL
print(user.created_at) # Account creation date
# Profile images
print(user.profile_image_url) # Profile picture URL
print(user.profile_banner_url) # Banner image URL
# Counts and metrics
print(user.followers_count) # Number of followers
print(user.following_count) # Number of following
print(user.statuses_count) # Number of tweets
print(user.favourites_count) # Number of likes
print(user.listed_count) # Times listed
print(user.media_count) # Media uploads count
# Verification status
print(user.verified) # Legacy verified (blue check)
print(user.is_blue_verified) # Twitter Blue subscriber
# Interaction permissions
print(user.can_dm) # Can you DM them
print(user.can_media_tag) # Can you tag them in media
# Other attributes
print(user.protected) # Is account private
print(user.possibly_sensitive) # Potentially sensitive content
print(user.description_urls) # URLs in bio
user = await client.follow_user('44196397')
print(f'Followed {user.screen_name}')
user = await client.unfollow_user('44196397')
print(f'Unfollowed {user.screen_name}')
user = await client.get_user_by_screen_name('example_user')
# Get followers
followers = await user.get_followers(count=20)
for follower in followers:
print(f'@{follower.screen_name} - {follower.name}')
# Get more followers
more_followers = await followers.next()
user = await client.get_user_by_screen_name('example_user')
# Get users they're following
following = await user.get_following(count=20)
for friend in following:
print(f'@{friend.screen_name}')
# Pagination
more_following = await following.next()
user = await client.get_user_by_screen_name('example_user')
verified_followers = await user.get_verified_followers(count=20)
for follower in verified_followers:
print(f'@{follower.screen_name} (verified: {follower.verified})')
user = await client.get_user_by_screen_name('example_user')
common_followers = await user.get_followers_you_know(count=20)
print('Followers you both know:')
for follower in common_followers:
print(f'@{follower.screen_name}')
user_id = '44196397'
# Get followers
followers = await client.get_user_followers(user_id, count=20)
# Get following
following = await client.get_user_following(user_id, count=20)
# Get verified followers
verified = await client.get_user_verified_followers(user_id, count=20)
# Get common followers
common = await client.get_user_followers_you_know(user_id, count=20)
# Get follower IDs (up to 5000 per request)
follower_ids = await client.get_followers_ids(
user_id='44196397',
count=5000
)
print(f'First 5 follower IDs: {follower_ids[:5]}')
# Get following IDs
following_ids = await client.get_friends_ids(
screen_name='elonmusk',
count=5000
)
# Pagination for large lists
more_ids = await follower_ids.next()
user = await client.block_user('123456789')
print(f'Blocked {user.screen_name}')
user = await client.unblock_user('123456789')
print(f'Unblocked {user.screen_name}')
user = await client.mute_user('123456789')
print(f'Muted {user.screen_name}')
user = await client.unmute_user('123456789')
print(f'Unmuted {user.screen_name}')
user = await client.get_user_by_screen_name('example_user')
subscriptions = await user.get_subscriptions(count=20)
for subscribed_user in subscriptions:
print(f'Subscribed to: @{subscribed_user.screen_name}')
# Search for users
results = await client.search_user('python developer', count=20)
for user in results:
print(f'@{user.screen_name}: {user.description}')
# Follow users with many followers
if user.followers_count > 1000:
await user.follow()
print(f'Followed @{user.screen_name}')
# Get more results
more_users = await results.next()
my_id = await client.user_id()
target_user = await client.get_user_by_screen_name('example_user')
# Get their following list and check if your ID is in it
following = await target_user.get_following(count=100)
following_ids = [user.id for user in following]
if my_id in following_ids:
print('They follow you back!')
else:
print('They do not follow you')
me = await client.user()
all_followers = []
followers = await me.get_followers(count=100)
while followers:
all_followers.extend(followers)
print(f'Fetched {len(all_followers)} followers...')
# Try to get more
try:
followers = await followers.next()
except:
break
# Save to file
import json
with open('followers.json', 'w') as f:
json.dump([
{'screen_name': u.screen_name, 'name': u.name, 'id': u.id}
for u in all_followers
], f, indent=2)
print(f'Exported {len(all_followers)} followers')
import asyncio
# Get a list of user IDs to follow
user_ids = ['123456', '789012', '345678']
for user_id in user_ids:
try:
user = await client.follow_user(user_id)
print(f'Followed @{user.screen_name}')
# Add delay to avoid rate limits
await asyncio.sleep(2)
except Exception as e:
print(f'Error following {user_id}: {e}')
user = await client.get_user_by_screen_name('example_user')
print(f'Followers: {user.followers_count}')
# Wait some time...
await asyncio.sleep(3600)
# Update to get fresh data
await user.update()
print(f'Updated followers: {user.followers_count}')