X API 通过user_name获取用户详情

import time import requests BEARER_TOKEN = "A" user_name = 'apex_ether' url = f"https://api.twitter.com/2/users/by/username/{user_name}" headers = {"Authorization": f"Bearer {BEARER_TOKEN}"} params = { "user.fields": "id,name,username,created_at,description,location,url,profile_image_url,protected,verified,public_metrics" } response = requests.get(url, headers=headers, params=params) time.sleep(5) # 即使查不到用户信息 响应码也是200,这里不会报错 response.raise_for_status() res_data = response.json() # {'data': {'location': 'Mempool', 'description': 'building @veildotcash', 'id': '1052847943185661952', 'name': 'Apex777.eth', 'username': 'apex_ether', 'profile_image_url': 'https://pbs.twimg.com/profile_images/1743420564436324352/eY2NN3QI_normal.jpg', 'public_metrics': {'followers_count': 6887, 'following_count': 939, 'tweet_count': 5250, 'listed_count': 81, 'like_count': 10249, 'media_count': 538}, 'url': 'https://t.co/nIOHUjJyDF', 'protected': False, 'verified': False, 'created_at': '2018-10-18T09:04:45.000Z'}} print(f'用户详情:{res_data}') # user_id = res_data.get('data', {}).get('id')

May 7, 2025 - 04:57
 0
X API 通过user_name获取用户详情

import time
import requests

BEARER_TOKEN = "A"

user_name = 'apex_ether'
url = f"https://api.twitter.com/2/users/by/username/{user_name}"
headers = {"Authorization": f"Bearer {BEARER_TOKEN}"}
params = {
    "user.fields": "id,name,username,created_at,description,location,url,profile_image_url,protected,verified,public_metrics"
}
response = requests.get(url, headers=headers, params=params)
time.sleep(5)
# 即使查不到用户信息 响应码也是200,这里不会报错
response.raise_for_status()
res_data = response.json()
# {'data': {'location': 'Mempool', 'description': 'building @veildotcash', 'id': '1052847943185661952', 'name': 'Apex777.eth', 'username': 'apex_ether', 'profile_image_url': 'https://pbs.twimg.com/profile_images/1743420564436324352/eY2NN3QI_normal.jpg', 'public_metrics': {'followers_count': 6887, 'following_count': 939, 'tweet_count': 5250, 'listed_count': 81, 'like_count': 10249, 'media_count': 538}, 'url': 'https://t.co/nIOHUjJyDF', 'protected': False, 'verified': False, 'created_at': '2018-10-18T09:04:45.000Z'}}
print(f'用户详情:{res_data}')
# user_id = res_data.get('data', {}).get('id')