xhs Documentation

repository·master·Indexed 22 days ago

https://github.com/reajason/xhs

A request tool that encapsulates Xiaohongshu web interfaces, providing access to the main site and Creator Services page. It includes the XhsClient for retrieving note and user information, managing comments, and performing social interactions. The project also provides xhs-api, a Flask-based signature service using Playwright to generate required x-s and x-t headers for API requests.

Tokens
2.2K
Snippets
10
Records
20
Agent score
81%

What's inside xhs

  1. Overview of the xhs library

    master
    xhs is a request tool that encapsulates the Xiaohongshu web interfaces. It provides access to both the main Xiaohongshu site (www.xiaohongshu.com) and the Creator Services page (creator.xiaohongshu.com).
  2. Advanced usage: Signature service architecture

    master

    For advanced usage and multi-account support, you can encapsulate playwright into a server-side service. The main application logic then uses requests to call this service to obtain signatures.

    Critical Requirement: When using a unified signature service across multiple accounts, ensure that the a1 field in the cookies is consistent across all accounts to avoid continuous signature errors.

  3. Install the latest version of xhs from GitHub

    master

    If you require the most recent features or fixes, you can install the package directly from the GitHub repository.

    $ python -m pip install git+https://github.com/ReaJason/xhs
  4. Install xhs and required dependencies

    master

    To use xhs, you must install the package along with playwright for browser simulation (used to execute JS for signature generation) and the stealth.min.js script to bypass environment detection.

    Follow these steps to set up your environment:

  5. Run the xhs signature service via Docker

    master

    You can run a Flask-based signature service using Docker. This service allows you to use XhsClient to request signatures. When the service starts, it will print the a1 value. It is highly recommended to ensure the a1 field in your cookies matches the a1 value set in the server to prevent signature errors.

    docker run -it -d -p 5005:5005 reajason/xhs-api:latest
  6. Build multi-architecture Docker images using buildx

    master

    To build Docker images that support multiple architectures (e.g., linux/arm64 and linux/amd64) simultaneously, use the docker buildx command. This requires creating a new builder instance with the docker-container driver.

    Follow these steps:

    1. Create a new builder named mybuilder using the docker-container driver.
    2. Switch to using the newly created mybuilder.
    3. Execute the build command specifying the target platforms with the --platform flag and push the resulting image using --push.
    docker buildx create --name mybuilder --driver docker-container
    
    docker buildx use mybuilder
    
    docker buildx build --platform linux/arm64,linux/amd64 -t reajason/xhs-api . --push
  7. Perform social interactions

    master

    Execute social actions like following users or interacting with content:

    User Interactions:

    • follow_user(user_id): Follow a user.
    • unfollow_user(user_id): Unfollow a user.

    Note Interactions:

    • collect_note(note_id): Save/collect a note.
    • uncollect_note(note_id): Remove a note from collections.
    • like_note(note_id): Like a note.
    • dislike_note(note_id): Unlike a note.

    Comment Interactions:

    • like_comment(note_id, comment_id): Like a specific comment.
    • dislike_comment(comment_id): Unlike a specific comment.
    # Examples
    xhs_client.follow_user("用户ID")
    xhs_client.unfollow_user("用户ID")
    xhs_client.collect_note("笔记ID")
    xhs_client.uncollect_note("笔记ID")
    xhs_client.like_note("笔记ID")
    xhs_client.dislike_note("笔记ID")
    xhs_client.like_comment("笔记ID", "评论ID")
    xhs_client.dislike_comment("评论ID")
  8. Handle QR code authentication

    master

    Use these methods to manage the QR code login flow:

    • get_qrcode(): Retrieve a new QR code for login.
    • check_qrcode(qrcode_id, qrcode_code): Check the status of a specific QR code to see if it has been scanned/authenticated.
    # Examples
    xhs_client.get_qrcode()
    xhs_client.check_qrcode("二维码ID", "二维码编码")