spider_xhs

repository·master·Indexed 27 days ago

https://github.com/cv-cat/spider_xhs

An all-in-one management and automation tool for Xiaohongshu (XHS) providing reverse-engineered API clients and a local PC signing runtime. It supports data collection from the PC side (notes, user info, search results), content publishing via the Creator platform, and accessing KOL data through the Pugongying and Qianfan platforms. The library handles complex signature algorithms (a1, web_id, x-s, x-t) and supports authentication via QR code, phone, or cookies.

Tokens
2.2K
Snippets
7
Records
13
Agent score
43%

What's inside spider_xhs

  1. Overview of Spider_XHS

    master

    Spider_XHS is an all-in-one manager for Xiaohongshu (XHS) designed to provide stable API support for AI agents. It handles the complex reverse-engineered signature algorithms (such as a1, web_id, x-s, x-t, etc.) required to interact with XHS. The project covers three main scenarios:

    1. Data Collection (PC side): Scraping notes, user info, search results, and comments.
    2. Content Publishing (Creator platform): Uploading images and videos to the creator platform.
    3. KOL Data (Pugongying platform): Accessing influencer data and managing collaborations.

    Warning: This project is for learning and exchange purposes only. Commercial use is strictly prohibited.

  2. Deploy Spider_XHS via Docker

    master

    You can build and run the project using Docker. If using cookie-based authentication, pass the COOKIES environment variable during runtime.

    docker build -t spider_xhs .
    docker run -e COOKIES='your_cookie_here' spider_xhs
  3. Integrate Spider_XHS into an AI Agent workflow

    master

    You can use Spider_XHS as a data foundation for AI-driven content operations. Common patterns include:

    • Competitor Scraping & Auto-Publishing: Scrape a note, use an AI model (GPT, Claude, etc.) to rewrite the content, and use the Creator APIs to post it.
    • Keyword Monitoring: Search for specific keywords and pass the results to an AI for trend analysis.
    • KOL Matching: Use the Pugongying API to fetch influencer lists and let an AI evaluate their suitability for a brand.
    from apis.xhs_pc_apis import XHS_Apis
    from apis.xhs_creator_apis import XHS_Creator_Apis
    from xhs_utils.xhs_pc import XHSPcAuth
    from xhs_utils.xhs_creator import XHSCreatorAuth
    
    # Initialize PC API for scraping
    pc_auth = XHSPcAuth.from_cookie(pc_cookie)
    pc_api = XHS_Apis(pc_auth).bootstrap()
    
    # Initialize Creator API for publishing
    creator_auth = XHSCreatorAuth.from_cookie(creator_cookie)
    creator_api = XHS_Creator_Apis(creator_auth).bootstrap()
    
    # 1. Scrape a note
    success, msg, note = pc_api.get_note_info(note_url)
    
    # 2. AI Rewrite (example placeholder)
    rewritten = your_ai_agent(note['content'])
    
    # 3. Auto-upload to Creator platform
    creator_api.post_note({
        "title": rewritten['title'],
        "desc": rewritten['desc'],
        "media_type": "image",
        "images": [...],
        ...
    })
  4. Install Spider_XHS dependencies

    master

    To set up the environment for Spider_XHS, ensure you have Python 3.10+ and Node.js 20+ installed. Then, install the Python and JavaScript dependencies using the following commands:

    pip install -r requirements.txt
    npm install
  5. Configure XHS login types

    master

    The project operates without a browser. You can configure the login method by setting the login_type variable in spider/spider.py. Supported types are:

    • qrcode: Local request for a QR code; scan with the Xiaohongshu App.
    • phone: Direct call to the phone number verification code interface.
    • cookie: Use a full Cookie string copied from a logged-in session.

    If using cookie mode, copy .env.example to .env and set the COOKIES variable:

    COOKIES='your_cookie_here'
  6. Authenticate for Creator API (Posting/Uploading)

    master

    Use XHSCreatorAuth to authenticate for the Creator platform (used for uploading and posting). You can choose between QR code, phone, or cookie-based authentication. Once authenticated, initialize XHS_Creator_Apis to access creator-specific endpoints.

    from apis.xhs_creator_apis import XHS_Creator_Apis
    from xhs_utils.xhs_creator import XHSCreatorAuth
    
    # Choose one of the three:
    creator_auth = XHSCreatorAuth.from_qrcode_login()
    # creator_auth = XHSCreatorAuth.from_phone_login()
    # creator_auth = XHSCreatorAuth.from_cookie(完整_creator_cookie)
    
    creator_api = XHS_Creator_Apis(creator_auth).bootstrap()
    success, message, notes = creator_api.get_all_posted_notes()
  7. Authenticate for PC API (Data Collection)

    master

    Use XHSPcAuth to manage login states for the PC interface (used for data collection). You can authenticate via QR code or by reusing existing cookies. Once authenticated, initialize XHS_Apis to access data endpoints.

    from apis.xhs_pc_apis import XHS_Apis
    from xhs_utils.xhs_pc import XHSPcAuth
    
    # No-browser QR code login
    auth = XHSPcAuth.from_qrcode_login()
    
    # Or reuse saved login Cookie
    # auth = XHSPcAuth.from_cookie(cookies_str)
    
    api = XHS_Apis(auth).bootstrap()
    success, message, data = api.get_unread_message()
  8. Reference: Project Structure

    master

    The repository is organized as follows:

    • spider/: Main entry point and crawler logic.
    • apis/: Complete API implementations for PC, Creator, Pugongying (KOL), and Qianfan (Distributor) platforms.
    • xhs_utils/xhs_pc/: Authentication, state management, and signature algorithms for the PC interface.
    • xhs_utils/xhs_creator/: Authentication, state management, and request assembly for the Creator platform.
    • xhs_utils/xhs_core/: Shared core algorithms (b1, MNS, X-s, etc.) used by both PC and Creator modules.
    • .env.example: Template for local configuration.
  9. Reference: Implemented XHS Features

    master

    The following modules and features are currently implemented:

    ModuleFeatureStatus
    Xiaohongshu PCQR Code / Mobile Login
    Get channels & recommended notes
    Get user profile / own account info
    Get user's posts / likes / favorites
    Get note details (no-watermark images & video)
    Search notes & users
    Get note comments
    Get unread messages / @mentions / likes / follows
    Creator PlatformQR Code / Mobile Login
    Session-level auto-retry (406 gate)
    Upload image collections
    Upload videos (includes transcoding polling)
    View published works list
    Creator RAP local computation
    Pugongying PlatformGet KOL lists & detailed data
    Get follower personas & historical trends
    Initiate collaboration invitations
    Qianfan PlatformGet distributor lists & detailed data
    Get distributor categories / shops / products
  10. Login to XHS Creator Center

    master

    Use the Creator platform functions to log in via QR code or phone verification to retrieve a list of all your posted works.

    Available methods:

    • creator_qrcode_login(): Performs a QR code login (displays QR in terminal) and fetches all posted notes.
    • creator_phone_login(): Performs a phone verification login and fetches all posted notes.