This commit is contained in:
mzl
2026-01-10 22:43:26 +08:00
parent 420c9b872a
commit 4c858d5074
+8 -47
View File
@@ -570,35 +570,14 @@ 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 = ""
xhs_no = ""
try:
full_text = (it.inner_text() or "").strip()
# 解析昵称和账号:从完整文本中提取
# 示例:青岛西门子家电规划师-海燕呀 1天前更新 小红书号:V13863940939 粉丝・52 笔记・217 关注
# 提取昵称:从开头到 "小红书号" 之前,去除时间信息
lines = full_text.split('\n')
main_text = lines[0] if lines else full_text
# 提取小红书号
m = re.search(r'小红书号[::]\s*([A-Za-z0-9]+)', main_text)
if m:
xhs_no = m.group(1).strip()
# 提取昵称:去除时间信息和账号信息
# 先移除小红书号部分
nickname_part = re.sub(r'小红书号[::]\s*[A-Za-z0-9]+.*', '', main_text)
# 移除时间信息(如 "1天前更新")
nickname_part = re.sub(r'\d+[天小时分秒]*前更新', '', nickname_part)
# 移除粉丝、笔记、关注信息
nickname_part = re.sub(r'粉丝[・・]\d+\s*笔记[・・]\d+\s*关注.*', '', nickname_part)
nickname = nickname_part.strip()
nickname = (it.inner_text() or "").strip()
except Exception:
nickname = ""
xhs_no = ""
else:
href = (it.locator("a[href^='/user/profile/']").first.get_attribute("href") or "").strip()
nickname = ""
@@ -618,35 +597,17 @@ def resolve_profile_by_search(
continue
score = 0
score_reasons = []
# 精确匹配小红书号:最高优先级
if xhs_no and xhs_no.lower() == kw.lower():
score += 1000
score_reasons.append("小红书号精确匹配")
# 精确匹配昵称:高优先级
if nickname and nickname.lower() == kw.lower():
score += 500
score_reasons.append("昵称精确匹配")
# 昵称包含关键词:中优先级(不使用 elif,允许累加)
if nickname and kw.lower() in nickname.lower() and nickname.lower() != kw.lower():
score += 50
score_reasons.append("昵称包含关键词")
# URL 中包含关键词:低优先级
if kw.lower() in href.lower():
score += 20
score_reasons.append("URL包含关键词")
# 有有效的 profile ID:基础分
score += 100
# 昵称包含关键词
if nickname and kw in nickname:
score += 10
# 有有效的 profile ID
if _extract_profile_id_from_href(href):
score += 5
score_reasons.append("有效Profile ID")
score += 1
reasons_str = ", ".join(score_reasons) if score_reasons else "无匹配"
_log(log, f"[账号] - 昵称: {nickname[:30] if len(nickname) > 30 else nickname}, 账号: {xhs_no}, 得分: {score} ({reasons_str})")
_log(log, f"[账号] - 昵称: {nickname[:30] if len(nickname) > 30 else nickname}, 账号: {xhs_no}, 得分: {score}")
if score > best_score:
best_score = score