{"record":{"id":"fb0ce16da2434b9b","repo":"xpzouying/xiaohongshu-mcp","slug":"user-userpagedata-value-not-found-in-initial-sta","errorCode":null,"errorMessage":"user.userPageData.value not found in __INITIAL_STATE__","messagePattern":"user\\.userPageData\\.value not found in __INITIAL_STATE__","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"xiaohongshu/user_profile.go","lineNumber":81,"sourceCode":"// extractUserProfileData 从页面中提取用户资料数据的通用方法\nfunc (u *UserProfileAction) extractUserProfileData(page *rod.Page, tab ProfileTab) (*UserProfileResponse, error) {\n\tpage.MustWait(`() => window.__INITIAL_STATE__ !== undefined`)\n\n\tuserDataResult := page.MustEval(`() => {\n\t\tif (window.__INITIAL_STATE__ &&\n\t\t    window.__INITIAL_STATE__.user &&\n\t\t    window.__INITIAL_STATE__.user.userPageData) {\n\t\t\tconst userPageData = window.__INITIAL_STATE__.user.userPageData;\n\t\t\tconst data = userPageData.value !== undefined ? userPageData.value : userPageData._value;\n\t\t\tif (data) {\n\t\t\t\treturn JSON.stringify(data);\n\t\t\t}\n\t\t}\n\t\treturn \"\";\n\t}`).String()\n\n\tif userDataResult == \"\" {\n\t\treturn nil, fmt.Errorf(\"user.userPageData.value not found in __INITIAL_STATE__\")\n\t}\n\n\t// 2. 获取用户帖子及当前 tab：window.__INITIAL_STATE__.user\n\tnotesResult := page.MustEval(`() => {\n\t\tconst u = window.__INITIAL_STATE__ && window.__INITIAL_STATE__.user;\n\t\tif (!u || !u.notes) return \"\";\n\t\tconst unwrap = (o) => (o && o.value !== undefined) ? o.value : (o && o._value);\n\t\tconst notes = unwrap(u.notes);\n\t\tif (!notes) return \"\";\n\t\tconst active = unwrap(u.activeTab) || {};\n\t\treturn JSON.stringify({notes: notes, index: active.index || 0, query: active.query || \"\"});\n\t}`).String()\n\n\tif notesResult == \"\" {\n\t\treturn nil, fmt.Errorf(\"user.notes.value not found in __INITIAL_STATE__\")\n\t}\n\n\t// 解析用户信息","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/xpzouying/xiaohongshu-mcp/blob/332d196854a9eac0d2b8c2c0e3d0cc43139d724c/xiaohongshu/user_profile.go#L63-L99","documentation":"extractUserProfileData 执行 JS 提取 window.__INITIAL_STATE__.user.userPageData（兼容 value/_value 解包）后返回空串：页面未注水用户资料数据——用户不存在、未登录或前端状态结构变化。","triggerScenarios":"调用 UserProfile 或 GetMyProfileViaSidebar 时，目标页面的 __INITIAL_STATE__ 尚未注入、user.userPageData 节点不存在或 value 为空——典型于页面未完成水合、被重定向到登录页、或用户主页不存在/被封。","commonSituations":"未登录访问受限主页；小红书改版移动了 __INITIAL_STATE__ 的数据路径；页面加载过慢，在 state 注入前就执行了 Eval；触发了风控验证码页。","solutions":["在调用前等待页面水合完成（等待用户头像/昵称等元素出现）","确认已登录且目标主页可正常访问（浏览器手动打开验证）","升级库版本适配最新的 state 结构","加重试：偶发的渲染时序问题通常重试即可"],"exampleFix":"// before\nres, err := client.UserProfile(ctx, userID, \"note\")\n// after\npage.MustWait(\"div.user-info, .nickname\") // 等水合\nres, err := client.UserProfile(ctx, userID, \"note\")\nif err != nil && strings.Contains(err.Error(), \"not found in __INITIAL_STATE__\") {\n    time.Sleep(2 * time.Second)\n    res, err = client.UserProfile(ctx, userID, \"note\")\n}","handlingStrategy":"retry","validationCode":"// 调用前确认用户主页可访问（HTTP 探测或手动验证）\nif !userProfileReachable(userID) {\n    return errors.New(\"用户主页不可访问，可能需要登录或用户不存在\")\n}","typeGuard":null,"tryCatchPattern":"res, err := client.UserProfile(ctx, userID, tab)\nif err != nil && strings.Contains(err.Error(), \"not found in __INITIAL_STATE__\") {\n    time.Sleep(2 * time.Second) // 等水合\n    res, err = client.UserProfile(ctx, userID, tab)\n}\nif err != nil { return err }","preventionTips":["确保处于有效登录态","访问前确认目标用户主页正常存在","对渲染时序问题做有限次重试","升级库版本适配 __INITIAL_STATE__ 结构变更","注意风控/验证码页会导致 state 缺失"],"tags":["scraping","dom","initial-state","hydration","go-rod"],"backgroundTag":"initial-state-missing","analyzedSha":"332d196854a9eac0d2b8c2c0e3d0cc43139d724c","analyzedAt":"2026-09-05T22:22:55.988Z","contentChangedAt":"2026-09-05T22:22:55.988Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}