diff --git a/xhs_crawler_core.py b/xhs_crawler_core.py index 6034187..5785d7f 100644 --- a/xhs_crawler_core.py +++ b/xhs_crawler_core.py @@ -575,9 +575,38 @@ def resolve_profile_by_search( nickname = "" xhs_no = "" try: - nickname = (it.inner_text() or "").strip() + full_text = (it.inner_text() or "").strip() + # 解析昵称和小红书号 + # 示例:青岛西门子家电规划师-海燕呀 1天前更新 小红书号:V13863940939 粉丝・52 笔记・217 关注 + + # 首先提取小红书号 + m = re.search(r'小红书号[::]\s*([A-Za-z0-9]+)', full_text) + if m: + xhs_no = m.group(1).strip() + + # 提取昵称:从开头去除干扰信息 + # 按行分割,通常第一行是昵称 + lines = full_text.split('\n') + if lines: + # 取第一行作为主要文本 + main_text = lines[0].strip() + # 移除小红书号及其后面的所有内容 + main_text = re.sub(r'小红书号[::].*$', '', main_text).strip() + # 移除时间信息(如 "1天前更新") + main_text = re.sub(r'\s+\d+[天小时分秒]+前更新.*$', '', main_text).strip() + # 移除粉丝、笔记、关注信息 + main_text = re.sub(r'\s+粉丝[・].*$', '', main_text).strip() + main_text = re.sub(r'\s+笔记[・].*$', '', main_text).strip() + main_text = re.sub(r'\s+关注.*$', '', main_text).strip() + # 移除分隔符 + main_text = re.sub(r'\s*[・·]\s*', ' ', main_text).strip() + # 清理多余空格 + nickname = re.sub(r'\s+', ' ', main_text).strip() + else: + nickname = full_text except Exception: nickname = "" + xhs_no = "" else: href = (it.locator("a[href^='/user/profile/']").first.get_attribute("href") or "").strip() nickname = ""