账号解析&接口相应性能

This commit is contained in:
mzl
2026-01-10 21:54:03 +08:00
parent e69df2ebb6
commit 65dfe594e0
2 changed files with 89 additions and 2 deletions
+18
View File
@@ -470,6 +470,15 @@ def resolve_profile_by_search(
page.goto(search_url, wait_until="domcontentloaded", timeout=60000)
page.wait_for_timeout(1200)
# 检查当前 URL,防止被重定向到个人主页
current_url = page.url
_log(log, f"[账号] 当前页面:{current_url}")
if "/user/profile/" in current_url and current_url != search_url:
# 被重定向到某个用户主页,这可能是搜索机制导致的
_log(log, f"[账号] 检测到重定向到用户主页,尝试返回搜索页")
page.goto(search_url, wait_until="domcontentloaded", timeout=60000)
page.wait_for_timeout(1500)
# ✅ 不再嵌套调用 check_login_state_with_storage(避免二次启动 playwright)
for t in ["扫码登录", "安全验证", "请先登录", "登录后查看"]:
try:
@@ -490,14 +499,19 @@ def resolve_profile_by_search(
try:
loc = page.locator(sel)
if loc.count() > 0:
_log(log, f"[账号] 找到用户标签:{sel}")
loc.first.click(timeout=3000)
page.wait_for_timeout(500)
return True
except Exception:
pass
try:
_log(log, f"[账号] 尝试通过角色定位用户标签")
page.get_by_role("tab", name="用户").click(timeout=3000)
page.wait_for_timeout(500)
return True
except Exception:
_log(log, f"[账号] 未找到用户标签,可能已经是用户搜索结果")
return False
def _wait_user_items() -> bool:
@@ -546,6 +560,7 @@ def resolve_profile_by_search(
items, use_anchor_only = _find_user_items()
n = items.count()
_log(log, f"[账号] 找到 {n} 个用户结果,使用模式:{'锚点链接' if use_anchor_only else '结构化元素'}")
if n == 0:
context.close(); browser.close()
raise RuntimeError("未找到用户结果(可能被拦截/需要更完整登录态)")
@@ -555,6 +570,7 @@ def resolve_profile_by_search(
for i in range(n):
it = items.nth(i)
_log(log, f"[账号] 分析第 {i+1}/{n} 个结果...")
if use_anchor_only:
href = (it.get_attribute("href") or "").strip()
nickname = ""
@@ -618,6 +634,8 @@ def resolve_profile_by_search(
if _extract_profile_id_from_href(href):
score += 5
_log(log, f"[账号] - 昵称: {nickname[:30] if len(nickname) > 30 else nickname}, 账号: {xhs_no}, 得分: {score}")
if score > best_score:
best_score = score
url = "https://www.xiaohongshu.com" + href if href.startswith("/") else href