Use this file to discover all available pages before exploring further.
This guide covers all operations related to Twitter users, including getting user information, following/unfollowing, blocking, muting, and managing followers.
# Get your own user informationme = await client.user()print(f'Logged in as: {me.screen_name}')print(f'Your user ID: {me.id}')# Or just get your user IDmy_id = await client.user_id()
The User object provides comprehensive information:
user = await client.get_user_by_screen_name('example_user')# Basic informationprint(user.id) # User IDprint(user.screen_name) # @usernameprint(user.name) # Display nameprint(user.description) # Bioprint(user.location) # Location stringprint(user.url) # Profile URLprint(user.created_at) # Account creation date# Profile imagesprint(user.profile_image_url) # Profile picture URLprint(user.profile_banner_url) # Banner image URL# Counts and metricsprint(user.followers_count) # Number of followersprint(user.following_count) # Number of followingprint(user.statuses_count) # Number of tweetsprint(user.favourites_count) # Number of likesprint(user.listed_count) # Times listedprint(user.media_count) # Media uploads count# Verification statusprint(user.verified) # Legacy verified (blue check)print(user.is_blue_verified) # Twitter Blue subscriber# Interaction permissionsprint(user.can_dm) # Can you DM themprint(user.can_media_tag) # Can you tag them in media# Other attributesprint(user.protected) # Is account privateprint(user.possibly_sensitive) # Potentially sensitive contentprint(user.description_urls) # URLs in bio
user = await client.get_user_by_screen_name('example_user')# Get followersfollowers = await user.get_followers(count=20)for follower in followers: print(f'@{follower.screen_name} - {follower.name}')# Get more followersmore_followers = await followers.next()
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}')
For faster retrieval of large follower lists, you can get just the IDs:
# 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 IDsfollowing_ids = await client.get_friends_ids( screen_name='elonmusk', count=5000)# Pagination for large listsmore_ids = await follower_ids.next()
Using ID methods is much faster than getting full user objects. Use this when you only need IDs or plan to fetch user details later.
Get users that someone is subscribed to (Twitter Premium feature):
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 usersresults = 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 resultsmore_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 itfollowing = 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 fileimport jsonwith 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 followuser_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}')
Be careful with bulk operations. Twitter has rate limits and may suspend accounts for aggressive following/unfollowing behavior.
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 dataawait user.update()print(f'Updated followers: {user.followers_count}')