Toggle navigation
Toggle navigation
This project
Loading...
Sign in
顾海波
/
xiaohongshu-skills
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Angiin
2026-03-08 01:15:54 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
3a8a8db1e93bee2eabaf6273905a0c77f451c89f
3a8a8db1
1 parent
f2106728
fix: 昵称读取改用个人主页 .user-name,修复导航栏返回固定文字'我'的问题
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
2 deletions
scripts/xhs/login.py
scripts/xhs/selectors.py
scripts/xhs/login.py
View file @
3a8a8db
...
...
@@ -28,6 +28,8 @@ from .selectors import (
PHONE_INPUT
,
PHONE_LOGIN_SUBMIT
,
QRCODE_IMG
,
USER_NICKNAME
,
USER_PROFILE_NAV_LINK
,
)
from
.urls
import
EXPLORE_URL
...
...
@@ -48,15 +50,32 @@ def _wait_for_auth_ui(page: Page, timeout: float = 8.0) -> None:
def
get_current_user_nickname
(
page
:
Page
)
->
str
:
"""获取当前登录用户的昵称,失败时返回空字符串(best-effort)。"""
"""获取当前登录用户的真实昵称,失败时返回空字符串(best-effort)。
流程:首页导航栏取个人主页 href → 导航过去 → 读 .user-name 文字。
"""
try
:
page
.
navigate
(
EXPLORE_URL
)
page
.
wait_for_load
()
_wait_for_auth_ui
(
page
)
if
not
page
.
has_element
(
LOGIN_STATUS
):
return
""
# 从导航栏"我"的链接取个人主页 URL(含 /user/profile/<user_id>)
profile_href
=
page
.
evaluate
(
f
"document.querySelector({json.dumps(USER_PROFILE_NAV_LINK)})?.getAttribute('href') || ''"
)
if
not
profile_href
:
return
""
# 导航到个人主页读取真实昵称
profile_url
=
f
"https://www.xiaohongshu.com{profile_href}"
page
.
navigate
(
profile_url
)
page
.
wait_for_load
()
page
.
wait_dom_stable
()
nickname
=
page
.
evaluate
(
f
"document.querySelector({json.dumps(
LOGIN_STATUS
)})?.innerText?.trim() || ''"
f
"document.querySelector({json.dumps(
USER_NICKNAME
)})?.innerText?.trim() || ''"
)
return
nickname
or
""
except
Exception
:
...
...
scripts/xhs/selectors.py
View file @
3a8a8db
...
...
@@ -91,3 +91,7 @@ LOGOUT_MENU_ITEM = 'div.menu-item[data-name="退出登录"]'
# ========== 用户主页 ==========
SIDEBAR_PROFILE
=
"div.main-container li.user.side-bar-component a.link-wrapper span.channel"
# 登录后导航栏"我"的链接(href 含 /user/profile/<user_id>)
USER_PROFILE_NAV_LINK
=
".main-container .user .link-wrapper a.link-wrapper"
# 个人主页真实昵称
USER_NICKNAME
=
".user-name"
...
...
Please
register
or
login
to post a comment