xpzouying/xiaohongshu-mcp · error

无法获取初始状态数据

Error message

无法获取初始状态数据

What it means

extractFeedDetail evaluates an in-page JS snippet to pull the initial-state JSON (window __INITIAL_STATE__). If the evaluation returns an empty string, it throws '无法获取初始状态数据'; the retry wrapper retries 3 times with backoff before giving up.

Source

Thrown at xiaohongshu/feed_detail.go:964

	// 使用retry-go来处理可能的DOM查询失败
	err := retry.Do(
		func() error {
			evalResult := page.MustEval(`() => {
				if (window.__INITIAL_STATE__ &&
					window.__INITIAL_STATE__.note &&
					window.__INITIAL_STATE__.note.noteDetailMap) {
					const noteDetailMap = window.__INITIAL_STATE__.note.noteDetailMap;
					return JSON.stringify(noteDetailMap);
				}
				return "";
			}`).String()

			if evalResult != "" {
				result = evalResult
				return nil
			}
			return fmt.Errorf("无法获取初始状态数据")
		},
		retry.Attempts(3),
		retry.Delay(200*time.Millisecond),
		retry.MaxJitter(300*time.Millisecond),
		retry.OnRetry(func(n uint, err error) {
			logrus.Debugf("提取Feed详情重试 #%d: %v", n, err)
		}),
	)

	if err != nil {
		logrus.Errorf("提取Feed详情失败: %v", err)
		return nil, fmt.Errorf("提取Feed详情失败: %w", err)
	}

	if result == "" {
		return nil, errors.ErrNoFeedDetail
	}

View on GitHub (pinned to 332d196854)

Solutions

  1. Increase wait/delay before extraction (wait for the state element or network idle)
  2. Verify the session isn't being redirected to a login/CAPTCHA page
  3. Update the JS extraction snippet if xiaohongshu renamed __INITIAL_STATE__
Defensive patterns

Strategy: retry

Try / catch

var detail *FeedDetailResponse
err := retry.Do(
    func() error { var e error; detail, e = client.GetFeedDetailWithConfig(ctx, id, cfg); return e },
    retry.Attempts(3), retry.DelayType(retry.BackOffDelay),
)

Prevention

When it happens

Trigger: Page loaded but __INITIAL_STATE__ is missing/empty — e.g. redirected to a login/verification page, SPA not hydrated yet, or a structurally changed page after a xiaohongshu frontend update.

Common situations: Anti-bot redirect to verify page; slow network where rod captured the DOM before state was injected; xiaohongshu changed the global state variable name.

Related errors


AI-assisted analysis of xpzouying/xiaohongshu-mcp@332d196854 (2026-09-05). Data as JSON: /api/errors/b879700a34d9acf6. Report an issue: GitHub.