XKit for Python
一个持续维护的 Twitter/X Web API Python 客户端。
本库提供的函数允许你进行对 Twitter/X 的操作,如发布或搜索推文,并且无需开发者 API 密钥。
特性
无需开发者 API 密钥
本库直接调用 Twitter/X 的 Web API 进行请求,无需申请官方开发者密钥。这里的“无需 API key”指无需 X 官方开发者密钥;可选的自动验证码解锁依赖第三方服务 Capsolver,需自备其 API key,见下文。
免费
本库本身免费;自动验证码求解依赖第三方付费服务 Capsolver,需自备 API key,详见下文。
功能
使用 XKit for Python,你可以:
-
创建推文
-
搜索推文
-
检索热门话题
-
等等...
安装
pip install xkit-py
PyPI 发行包名是 xkit-py,Python 导入包名是 xkit;新代码请使用 xkit import 路径:
from xkit import Client
旧的上游兼容 import 路径仍然可用:
from twikit import Client
ui_metrics 与验证码解锁
ui_metrics 默认开启。当会话触发 326 锁号并进行账号解锁时,客户端会下载 X 的混淆 JavaScript,
通过 Js2Py(模块名 js2py_)在当前 Python 进程内执行,并将计算结果提交给 X。
Js2Py 不是安全沙箱;远端脚本或其下载链路被篡改时,执行脚本可能影响宿主进程,
存在沙箱逃逸或任意代码执行的风险。本库没有为这段 JS 提供独立隔离或执行超时。
如需关闭手动解锁和自动解锁中的 JS 执行,在构造客户端时设置:
client = Client('en-US', enable_ui_metrics=False)
关闭后不会下载或执行对应的 ui_metrics 脚本;X 可能因此要求额外验证或拒绝解锁。
自动验证码求解依赖第三方付费服务 Capsolver,需单独提供 API key。
Capsolver(api_key=..., timeout=120.0) 使用异步请求和等待;每次求解的总超时包含
创建任务、HTTP 请求与轮询,超时抛出 RequestTimeout。
任务默认使用 FunCaptchaTaskProxyLess,不会把 Client 的代理 URL 或代理凭据交给该服务。
只有显式设置 Capsolver(api_key=..., use_client_proxy=True) 才会将配置的代理 URL
(包括其中的账号密码)发送给 Capsolver;启用前请确认愿意向该服务披露这些信息。
使用样例
使用 Token / Cookie 初始化客户端
账密登录(login())已移除,请使用浏览器会话中的 auth_token + ct0 或导出的 Cookie JSON 文件:
import asyncio
from xkit import Client
# 方式一:构造时直接传入 auth_token 和 ct0
client = Client(
'en-US',
auth_token='your_auth_token',
ct0='your_ct0_token',
)
# 方式二:构造时直接加载 cookies.json(支持 {name: value} 字典或浏览器插件导出的 Cookie 列表)
# client = Client('en-US', cookies_file='cookies.json')
async def main():
user = await client.user()
print(user.name)
asyncio.run(main())
创建一条附带媒体的推文
# 上传媒体文件并获取媒体ID
media_ids = [
await client.upload_media('media1.jpg'),
await client.upload_media('media2.jpg')
]
# 创建一条带有提供的文本和附加媒体的推文
await client.create_tweet(
text='Example Tweet',
media_ids=media_ids
)
搜索推文
tweets = await client.search_tweet('python', 'Latest')
for tweet in tweets:
print(
tweet.user.name,
tweet.text,
tweet.created_at
)
检索用户的推文
tweets = await client.get_user_tweets('123456', 'Tweets')
for tweet in tweets:
print(tweet.text)
获取趋势
await client.get_trends('trending')
Metadata
Release files for xkit-py 3.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| xkit_py-3.0.1.tar.gz | 110.1 kB | With provenance | Details for xkit_py-3.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| xkit_py-3.0.1-py3-none-any.whl | Python 3 | none | any | Details for xkit_py-3.0.1-py3-none-any.whl |
Total release size: 214.8 kB
Release files / xkit_py-3.0.1.tar.gz
| Download URL | xkit_py-3.0.1.tar.gz |
|---|---|
| Size | 110.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4b14dbcb3b37ff6aac16c010f5bbabd6be8c41bd21823c87e012a3a28f3ee0d9
|
|
BLAKE2b-256 checksum How to use checksums |
351c1619930d574aeb8f506f69fda729891eabed866e47925395c4a0bbe5f0b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.
Transparency logRelease files / xkit_py-3.0.1-py3-none-any.whl
| Download URL | xkit_py-3.0.1-py3-none-any.whl |
|---|---|
| Size | 104.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
090f35a4d5ea79714011207b9a2dabf240f9a72e9b56144aef35dce5a2b86075
|
|
BLAKE2b-256 checksum How to use checksums |
ca405dd043c15ec0c620704480961e4b6c016ab6b32ea97716ea03b1f934c4f4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.
Transparency logSecurity for xkit-py 3.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for xkit_py-3.0.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.