如何通过真实姓名在Tweepy中查询Twitter账号?
Got it, let's break this down. The reason your get_user(name='George Takei') call failed is totally intentional—Twitter's API (and Tweepy's wrapper for it) doesn't let you look up users directly by their display name (the "name" field) because that field isn't unique. Hundreds of people could share the exact same real name, so there's no way to map that to a single user account reliably.
That said, you can use Twitter's user search functionality to find accounts matching a given real name. Here are two reliable ways to do this with Tweepy:
1. Use Tweepy's search_users() (v1.1 API)
The search_users() method lets you search for users using a query string, which checks both display names and screen names. You can then sift through the results to find the most relevant matches.
Here's a working example:
import tweepy # First, authenticate with your Twitter dev credentials auth = tweepy.OAuthHandler("YOUR_CONSUMER_KEY", "YOUR_CONSUMER_SECRET") auth.set_access_token("YOUR_ACCESS_TOKEN", "YOUR_ACCESS_TOKEN_SECRET") api = tweepy.API(auth) # Search for users matching the real name (adjust count to get more/less results) target_name = "George Takei" matching_users = api.search_users(q=target_name, count=5) # Loop through results to see potential matches for user in matching_users: print(f"Screen Name: @{user.screen_name} | Display Name: {user.name} | Follower Count: {user.followers_count}")
Things to note here:
- The
countparameter sets how many results you get (max 20 for v1.1). - You'll likely get multiple results for common names—add filters to narrow things down (e.g., check for exact name matches, prioritize accounts with high follower counts, or look for verified badges with
user.verified). - Keep an eye on rate limits: v1.1 allows 180
search_usersrequests per 15-minute window.
2. Use Twitter API v2 with Tweepy (Recommended for New Projects)
If you're starting a fresh project, go with Twitter's v2 API—it's more flexible and has better rate limits. Tweepy supports v2 via the Client class.
Here's how to search specifically by display name with v2:
import tweepy # Authenticate with your v2 bearer token client = tweepy.Client(bearer_token="YOUR_BEARER_TOKEN") # Use the `name:` operator to target display names exclusively target_name = "George Takei" response = client.search_users(query=f"name:{target_name}", max_results=5) # Process the returned user data if response.data: for user in response.data: print(f"Screen Name: @{user.username} | Display Name: {user.name} | Verified: {user.verified}")
Why v2 is better:
- Advanced search operators let you narrow results (e.g., add
verified:trueto only get verified accounts matching the name). - Higher rate limits: 900 user search requests per 15-minute window.
- Cleaner, more structured response data.
Critical Limitations to Remember
- Accuracy: There's no way to guarantee you'll find the exact person you're looking for, especially with common names. You'll need to build extra logic (like checking bio keywords, location, or follower count) to prioritize the right account.
- API Access: You'll need a Twitter Developer account to get the necessary keys/tokens for either API version.
- Rate Limits: Always respect Twitter's rate limits to avoid getting your access restricted.
内容的提问来源于stack exchange,提问作者Marcelo Soares

