diff --git a/services/search_service.py b/services/search_service.py index 99dd5cf..12978a6 100644 --- a/services/search_service.py +++ b/services/search_service.py @@ -210,15 +210,35 @@ class SearchService: def _extract_text_from_snapshot(self, snapshot): """从 accessibility tree snapshot 中提取文本内容""" - # 提取所有 StaticText 行 texts = [] + for line in snapshot.split('\n'): - if 'StaticText' in line: - # 格式: - StaticText "文本内容" - match = re.search(r'StaticText "([^"]+)"', line) - if match: - texts.append(match.group(1)) - return '\n'.join(texts) + line = line.strip() + if 'StaticText' in line and 'checkbox' not in line: + # 找到 StaticText 后的内容 + idx = line.find('StaticText') + after = line[idx + 10:].strip() # 跳过 'StaticText' + + # 去掉开头的引号 + if after.startswith('"'): + after = after[1:] + + # 如果以 JSON 开头(错误信息),跳过 + if after.startswith('{'): + continue + + # 提取文本内容 + text = after.rstrip('"').strip() + if text and len(text) > 1: + texts.append(text) + + result = '\n'.join(texts) + + # 检测是否是反爬错误页面 + if '请求存在异常' in result or '暂时限制本次访问' in result: + return '[该网站触发了反爬机制,无法抓取内容]' + + return result def search_articles(self, keyword, category=None): """从内容库搜索"""