You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过真实姓名在Tweepy中查询Twitter账号?

How to Find Twitter Users by Real Name (Display Name) Using Tweepy

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 count parameter 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_users requests per 15-minute window.

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:true to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 10:16:33