ナビゲーションにスキップ

Use Python to find Xiaohongshu notes, read selected comments and organize product questions

To turn Xiaohongshu discussion into product questions with Python, save one page of notes from data:xiaohongshu.app_v2.search_notes, select the discussions worth reading, and save comments for the selected notes from data:xiaohongshu.app_v2.get_note_comments. Then run the existing comment exporter to produce a CSV and themes backed by original wording.

The example starts with “coffee machine.” Search once, then select notes yourself. If you already have a verified note ID, skip the search and start with comments.

1. Prepare the environment and scope

Use Python 3 and the standard library; no third-party SDK is needed. Set your key locally as the BEATAPI_API_KEY environment variable; the script does not write authorization headers to files.

Plan one search page plus one comment page per selected note. Three selected notes mean at most four data requests. Estimate the cost with current pricing; the actual comment count comes from each response.

2. Save one request and response

Save the script below as xhs_read.py. The search command calls data:xiaohongshu.app_v2.search_notes; the comments command calls data:xiaohongshu.app_v2.get_note_comments. Each run makes one request and requires a new output directory.

import json
import os
import sys
from datetime import datetime, timezone
from pathlib import Path
from urllib.error import HTTPError
from urllib.request import Request, urlopen
if len(sys.argv) != 4 or sys.argv[1] not in {"search", "comments"}:
raise SystemExit("Usage: python3 xhs_read.py search|comments VALUE NEW_DIRECTORY")
mode, value, directory = sys.argv[1:]
if not value.strip():
raise SystemExit("Supply a keyword or a verified note ID")
key = os.environ.get("BEATAPI_API_KEY")
if not key:
raise SystemExit("Set BEATAPI_API_KEY in your local environment")
if mode == "search":
action = "xiaohongshu.app_v2.search_notes"
params = {"keyword": value, "page": 1}
else:
action = "xiaohongshu.app_v2.get_note_comments"
params = {"note_id": value, "cursor": "", "index": 0,
"pageArea": "UNFOLDED", "sort_strategy": "latest_v2"}
body = {"action": action, "params": params}
out = Path(directory)
out.mkdir(parents=True, exist_ok=False)
(out / "request.json").write_text(json.dumps(body, ensure_ascii=False, indent=2), encoding="utf-8")
stamp = datetime.now(timezone.utc).isoformat()
(out / "attempt.json").write_text(json.dumps({"started_at": stamp, "action": action}, indent=2), encoding="utf-8")
request = Request("https://api.beatapi.io/v1/social-data/call",
data=json.dumps(body).encode("utf-8"), method="POST",
headers={"Authorization": "Bearer " + key, "Content-Type": "application/json"})
try:
with urlopen(request, timeout=60) as response:
status, raw = response.status, response.read()
except HTTPError as error:
status, raw = error.code, error.read()
(out / "response.json").write_bytes(raw)
(out / "call.json").write_text(json.dumps({"action": action, "params": params,
"http_status": status, "collected_at": stamp, "response_file": "response.json"},
ensure_ascii=False, indent=2), encoding="utf-8")
payload = json.loads(raw)
if status != 200 or not isinstance(payload, dict) or payload.get("status") != "succeeded" or payload.get("error") is not None or payload.get("action") != action:
raise SystemExit("Request failed or returned an unexpected result; inspect saved response")
inner = payload.get("data")
if not isinstance(inner, dict) or inner.get("success") is not True or type(inner.get("code")) is not int or inner["code"] != 0:
raise SystemExit("Business result is not successful; retain saved files for review")
if mode == "comments":
pages = [{"action": action, "note_id": value, "collected_at": stamp, "file": "response.json"}]
(out / "comment-pages.json").write_text(json.dumps(pages, ensure_ascii=False, indent=2), encoding="utf-8")
print("Saved response: " + str(out / "response.json"))

Retrieve the first search page:

python3 xhs_read.py search 'coffee machine' search-call-1

Keep request.json, attempt.json, call.json and response.json from each run. xhs_read.py also saves received error responses for diagnosis. If a timeout leaves no final result, review the request record before deciding whether to resend.

3. Select relevant notes

Search results list notes at data.data.items[].note. Review the actual titles and excerpts, and for each note with a concrete usage situation keep note.id and a selection reason. Advertising elements, outer result IDs and display names are not note IDs.

For cleaning research, select notes that actually discuss cleaning, not notes that only show equipment. To read the author’s returned detail text, use the selected-note workflow; a search excerpt is not the whole material.

4. Read selected comments

Copy the actual note.id of each selected note and use a separate output directory for each note:

python3 xhs_read.py comments '<selected_note_id>' comments-call-1

Verified root comments are at data.data.comments. Read the wording, times and context before organizing product questions. Keep embedded reply previews apart from separately retrieved replies; the complete comment workflow covers further pages.

5. Produce CSV and reviewed themes

After a successful comment request, xhs_read.py writes comment-pages.json for the existing exporter. Save comment_research.py from the comment guide in the working directory, then extract the comments locally:

python3 comment_research.py extract comments-call-1/comment-pages.json comment-review

For several saved notes, combine the manifest records into one array, and make each file path relative to the combined manifest’s actual location. Keep each note ID and query time.

Read comments.csv, review labels.json item by item, then produce themes.csv, evidence.json and coverage.json. Use actual excerpts, and count supporting comments separately from distinct notes. A bounded collection does not represent every user.

Deliver questions such as unclear cleaning steps or compatibility checks, each with the wording, the note source and a next action. Colleagues can act on the question list directly instead of reading raw JSON.

For example, if two different notes each have a comment asking why an odor remains after switching tablets, put one troubleshooting theme in themes.csv and record two supporting comments from two notes. Check the wording and conditions in evidence.json before adding advice to the cleaning guide.

Reuse saved responses. xhs_read.py does not select notes, expand pages or reply to commenters. Decide the additional scope before retrieving more material.

Search and selection · Comments to themes