From d23d526629559b6f97d6e65f3c233fcf154f28ff Mon Sep 17 00:00:00 2001 From: hz4th_coder Date: Mon, 13 Jul 2026 23:51:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=90=9C=E7=B4=A2=E5=92=8C?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E6=A3=80=E6=9F=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 搜索文章时增加URL字段搜索,解决重复抓取问题 2. 优化搜索结果解析,多获取几个结果确保返回足够数量 --- models/database.py | 8 ++++---- services/search_service.py | 13 +++++++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/models/database.py b/models/database.py index 38ce08b..89cb3a0 100644 --- a/models/database.py +++ b/models/database.py @@ -157,16 +157,16 @@ class Database: if category: cursor.execute(''' SELECT * FROM articles - WHERE (product_names LIKE ? OR keywords LIKE ? OR summary LIKE ? OR content LIKE ?) + WHERE (product_names LIKE ? OR keywords LIKE ? OR summary LIKE ? OR content LIKE ? OR url LIKE ?) AND category = ? ORDER BY fetch_date DESC - ''', (f'%{keyword}%', f'%{keyword}%', f'%{keyword}%', f'%{keyword}%', category)) + ''', (f'%{keyword}%', f'%{keyword}%', f'%{keyword}%', f'%{keyword}%', f'%{keyword}%', category)) else: cursor.execute(''' SELECT * FROM articles - WHERE product_names LIKE ? OR keywords LIKE ? OR summary LIKE ? OR content LIKE ? + WHERE product_names LIKE ? OR keywords LIKE ? OR summary LIKE ? OR content LIKE ? OR url LIKE ? ORDER BY fetch_date DESC - ''', (f'%{keyword}%', f'%{keyword}%', f'%{keyword}%', f'%{keyword}%')) + ''', (f'%{keyword}%', f'%{keyword}%', f'%{keyword}%', f'%{keyword}%', f'%{keyword}%')) return [dict(row) for row in cursor.fetchall()] def get_article_by_id(self, article_id): diff --git a/services/search_service.py b/services/search_service.py index 6635cea..63c45eb 100644 --- a/services/search_service.py +++ b/services/search_service.py @@ -157,12 +157,17 @@ class SearchService: if match: title = match.group(1) ref = match.group(2) - # 放宽过滤条件:只要不是纯域名格式就保留 - if not (title.endswith('.com') or title.endswith('.cn') or title.endswith('.net')): + # 过滤纯域名格式 + is_domain = (title.endswith('.com') or title.endswith('.cn') or + title.endswith('.net') or title.endswith('.org') or + title.endswith('.edu') or title.endswith('.gov')) + if not is_domain: refs.append((title, ref)) - # 获取每个结果的 URL - for title, ref in refs[:max_results]: + # 获取每个结果的 URL,多获取几个以防解析失败 + for title, ref in refs[:max_results + 5]: + if len(results) >= max_results: + break url = self._get_link_url(ref) if url and 'bing.com/search' not in url: # 过滤搜索结果页本身的链接 results.append({