Compare commits

..
1 Commits
Author SHA1 Message Date
hz4th_coder 9d6481bd06 修复页面内容提取,过滤 JSON 错误信息
- 改进 StaticText 解析逻辑
- 过滤知乎等网站的反爬 JSON 错误
- 正确提取页面文本内容
2026-07-13 16:56:08 +08:00
+27 -7
View File
@@ -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):
"""从内容库搜索"""