DEV Community

mian po
mian po

Posted on • Originally published at xhs-data-api.pages.dev

Retrieve Rich Xiaohongshu Note Data With Python

Retrieve Rich Xiaohongshu Note Data With Python

This tutorial retrieves normalized public note data from a complete Xiaohongshu (RedNote) URL through RapidAPI.

What You Need

  • A subscription to XHS Data API.
  • A server-side RapidAPI key.
  • A current public note URL containing the xsec_token copied with that note.

Keep both values in environment variables:

export X_RAPIDAPI_KEY="your_rapidapi_key"
export XHS_NOTE_URL="complete_current_public_note_url"
Enter fullscreen mode Exit fullscreen mode

Validate The URL First

/note/parse_url performs local validation and extracts URL components without fetching upstream note data:

import os
import requests

base_url = "https://xiaohongshu-rednote-data-api.p.rapidapi.com"
headers = {
    "X-RapidAPI-Key": os.environ["X_RAPIDAPI_KEY"],
    "X-RapidAPI-Host": "xiaohongshu-rednote-data-api.p.rapidapi.com",
}

parsed = requests.get(
    f"{base_url}/note/parse_url",
    headers=headers,
    params={"url": os.environ["XHS_NOTE_URL"], "raw": "false"},
    timeout=30,
)
parsed.raise_for_status()
print(parsed.json()["data"]["note_id"])
Enter fullscreen mode Exit fullscreen mode

Retrieve Normalized Detail

detail = requests.get(
    f"{base_url}/note/detail_from_url",
    headers=headers,
    params={"url": os.environ["XHS_NOTE_URL"], "raw": "false"},
    timeout=45,
)
detail.raise_for_status()
data = detail.json()["data"]
note = data["note"]

print("title:", note.get("title"))
print("author:", (note.get("author") or {}).get("nickname"))
print("type:", note.get("type"))
print("images:", len(note.get("images") or []))
print("request_id:", data["request_id"])
Enter fullscreen mode Exit fullscreen mode

The normalized note contract can include author identity, description, engagement counts, tags, timestamps, location, images, video, music, and media dimensions when the public source supplies them.

Understand Missing Fields

null means the selected upstream page did not provide that field. It is not zero. Optional location, music, video, or engagement fields can legitimately be unavailable.

Media URLs may be signed or temporary. Use them promptly and do not treat them as permanent storage.

Handle Failures

Keep data.request_id from every error. A retryable 503 REQUEST_TIMEOUT may include Retry-After; wait for that interval before retrying the identical request. When a note token has expired, obtain a current complete public URL instead of inventing or transferring a token.

The complete Python and Node.js clients are available on the developer website and in the public examples repository.

XHS Data API is independent and is not affiliated with or endorsed by Xiaohongshu/RedNote. Use public data lawfully and follow applicable law, third-party rights, and source-platform rules.

Top comments (0)