twikit

d60MIT更新放缓

无需 API Key 即可抓取 Twitter 数据的 Python 库,直接调用 Twitter 内部接口,支持发推、搜索、爬取用户信息,还能做机器人。

Twitter API Scraper | Without an API key | Twitter Internal API | Free | Twitter scraper | Twitter Bot

version2.3.1发布于 2025-02-06 15:56 UTC4,771star +7 / 7 天575fork同步于 2026-11-10 11:37 UTC

怎么上手

由 README 提炼,执行前请确认
pip install twikit
硬件要求按 README 说明

README 未说明,纯 Python 库无 GPU 或显存要求

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

无需 API Key 的 Twitter 爬虫库,用 Python 直接发推、搜推和抓趋势

无需 API Key 即可抓取 Twitter 数据的 Python 库,直接调用 Twitter 内部接口,支持发推、搜索、爬取用户信息,还能做机器人。

核心能力

  • 通过模拟登录获取 cookies 调用 Twitter 内部接口
  • 支持发布带图片的推文并上传媒体文件
  • 按关键词搜索最新推文并读取用户推文
  • 可发送私信、获取热门趋势话题
  • 提供异步 Client 接口,登录态可存 cookies 文件复用

容易踩的坑

需要真实 Twitter 账号的用户名、邮箱和密码登录,无账号无法使用;登录态依赖 cookies 文件,需自行保管避免泄露;使用非官方内部接口,账号存在被限流或封禁风险

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本version2.3.1
  1. version2.3.1Version 2.3.1最新正式版2025-02-06 · 2025-02-06 15:56 UTC

    Version 2.3.1

    The method for solving ui_metrics has been changed. Previously, it relied on node.js and jsdom, but now it uses Js2Py instead, eliminating the need for node.js. (For more details about ui_metrics, refer to the v2.3.0 release page.)

    Additionally, the default of the enable_ui_metrics argument in Client.login has been changed to True.

    The new version has been tested and confirmed to work on Python 3.10, 3.11, 3.12, and 3.13, but please let us know if you encounter any errors.

  2. version2.3.0version 2.3.02025-02-04 · 2025-02-04 18:40 UTC
    查看更新说明

    Version 2.3.0

    Major Updates

    1. Changes to Tweet.media

    Previously, Tweet.media directly stored the response returned by the API. Starting from v2.3.0, it now returns a list containing instances of the newly introduced Photo, AnimatedGif, and Video classes. This makes media downloads easier, and for Video, subtitle downloads are also supported.

    https://twikit.readthedocs.io/en/latest/twikit.html#twikit.tweet.Tweet.media https://github.com/d60/twikit/blob/main/examples/download_tweet_media.py

    2. Added cookies_file argument to Client.login

    This parameter simplifies saving and loading cookies. Previously, you had to write:

    if os.path.exists('cookies.json'):
        client.load_cookies('cookies.json')
    else:
        await client.login(
            auth_info_1=...,
            auth_info_2=...,
            password=...
        )
        client.save_cookies('cookies.json')
    

    Now, you can just write:

    await client.login(
        auth_info_1=..., 
        auth_info_2=..., 
        password=..., 
        cookies_file='cookies.json',
    )
    

    https://twikit.readthedocs.io/en/latest/twikit.html#twikit.client.client.Client.login

    3. Added enable_ui_metrics argument to Client.login

    When set to True, the obfuscated ui_metrics function is executed using JSDom, and the result is sent to the API. Enabling this might reduce the risk of account suspension. To use this feature, Node.js and JSDom must be installed. If Node.js is available in your environment, enabling this option is recommended.

    Usage:

    await client.login(
        auth_info_1=a1,
        auth_info_2=a2,
        password=password,
        cookies_file='cookies.json',
        enable_ui_metrics=True
    )
    

    https://twikit.readthedocs.io/en/latest/twikit.html#twikit.client.client.Client.login

  3. version2.2.2Version 2.2.22025-01-29 · 2025-01-29 15:04 UTC
    查看更新说明
    • Implemented transaction ID in guest client requests and fixed 404 error
    • Removed the dependency on requests (https://github.com/d60/twikit/pull/270)
    • Fixed a bug in login with TOTP
    • Added Tweet.bookmark_count and Tweet.bookmarked
  4. version2.2.0Version 2.2.02024-12-01 · 2024-12-01 03:55 UTC
    查看更新说明

    Version 2.2.0

    • Implemented transaction id.
    • Fixed error in create_tweet.
    • Fixed login error (flow name LoginFlow is currently not accessible).

    python -m pip install -U twikit -U lxml

  5. version2.1.3Version 2.1.32024-08-23 · 2024-08-23 15:14 UTC
    查看更新说明

    Version 2.1.3

    • Fixed KeyError: 'open_account'"
    • flow name LoginFlow is currently not accessible has not yet been fixed.

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    [!NOTE] https://github.com/d60/twitter_login (under development)

    Number of GitHub stars GitHub commit activity Version Tweet Discord BuyMeACoffee

    [日本語] [中文]

    Twikit

    A Simple Twitter API Scraper

    You can use functions such as posting or searching for tweets without an API key using this library.

    🔵 Discord

    [!NOTE] Released twikit_grok an extension for using Grok AI with Twikit.
    For more details, visit: https://github.com/d60/twikit_grok.

    Features

    No API Key Required

    This library uses scraping and does not require an API key.

    Free

    This library is free to use.

    Functionality

    By using Twikit, you can access functionalities such as the following:

    • Create tweets

    • Search tweets

    • Retrieve trending topics

    • etc...

    Installing

    
    pip install twikit
    
    

    Quick Example

    Define a client and log in to the account.

    import asyncio
    from twikit import Client
    
    USERNAME = 'example_user'
    EMAIL = '[email protected]'
    PASSWORD = 'password0000'
    
    # Initialize client
    client = Client('en-US')
    
    async def main():
        await client.login(
            auth_info_1=USERNAME,
            auth_info_2=EMAIL,
            password=PASSWORD,
            cookies_file='cookies.json'
        )
    
    asyncio.run(main())
    

    Create a tweet with media attached.

    # Upload media files and obtain media_ids
    media_ids = [
        await client.upload_media('media1.jpg'),
        await client.upload_media('media2.jpg')
    ]
    
    # Create a tweet with the provided text and attached media
    await client.create_tweet(
        text='Example Tweet',
        media_ids=media_ids
    )
    
    

    Search the latest tweets based on a keyword

    tweets = await client.search_tweet('python', 'Latest')
    
    for tweet in tweets:
        print(
            tweet.user.name,
            tweet.text,
            tweet.created_at
        )
    

    Retrieve user tweets

    tweets = await client.get_user_tweets('123456', 'Tweets')
    
    for tweet in tweets:
        print(tweet.text)
    

    Send a dm

    await client.send_dm('123456789', 'Hello')
    

    Get trends

    await client.get_trends('trending')
    

    More Examples: examples

    Contributing

    If you encounter any bugs or issues, please report them on issues.

    If you find this library useful, consider starring this repository⭐️