Compare commits

...
2 Commits
Author SHA1 Message Date
hz4th_coder 76e24c4fa3 新增搜索标题和网页标题双字段
- 数据库新增 search_title 字段存储搜索结果标题
- product_names 存储抓取到的网页标题
- 搜索页面显示两个标题(搜索标题 + 网页标题)
- 内容库页面也显示两个标题
2026-07-14 00:00:50 +08:00
hz4th_coder d23d526629 修复搜索和重复检查问题
1. 搜索文章时增加URL字段搜索,解决重复抓取问题
2. 优化搜索结果解析,多获取几个结果确保返回足够数量
2026-07-13 23:51:23 +08:00
7 changed files with 70 additions and 18 deletions
+16 -9
View File
@@ -32,6 +32,7 @@ class Database:
CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
product_names TEXT NOT NULL,
search_title TEXT,
category TEXT,
keywords TEXT,
summary TEXT,
@@ -44,6 +45,12 @@ class Database:
)
''')
# 为旧表添加字段(如果不存在)
try:
cursor.execute('ALTER TABLE articles ADD COLUMN search_title TEXT')
except:
pass
# 待处理产品列表
cursor.execute('''
CREATE TABLE IF NOT EXISTS pending_products (
@@ -138,15 +145,15 @@ class Database:
conn.commit()
# ========== 内容库操作 ==========
def add_article(self, product_names, category, keywords, summary, content, source, url=None):
def add_article(self, product_names, category, keywords, summary, content, source, url=None, search_title=None):
"""添加文章到内容库"""
with self.get_connection() as conn:
cursor = conn.cursor()
cursor.execute('''
INSERT INTO articles (product_names, category, keywords, summary, content, source, url)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', (json.dumps(product_names, ensure_ascii=False), category,
json.dumps(keywords, ensure_ascii=False), summary, content, source, url))
INSERT INTO articles (product_names, search_title, category, keywords, summary, content, source, url)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
''', (json.dumps(product_names, ensure_ascii=False), search_title,
category, json.dumps(keywords, ensure_ascii=False), summary, content, source, url))
conn.commit()
return cursor.lastrowid
@@ -157,16 +164,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):
+4 -1
View File
@@ -108,6 +108,7 @@ def fetch_article():
"""从URL抓取文章"""
data = request.get_json()
url = data.get('url')
search_title = data.get('search_title') # 搜索结果的标题
if not url:
return jsonify({'error': '请提供URL'}), 400
@@ -116,6 +117,7 @@ def fetch_article():
if result and result.get('success'):
# 自动保存到内容库
# product_names 使用抓取到的标题,search_title 使用搜索结果的标题
article_id = search_service.save_to_articles(
product_names=data.get('product_names', [result['title']]),
category=data.get('category'),
@@ -123,7 +125,8 @@ def fetch_article():
summary=result.get('description', ''),
content=result['content'],
source=url,
url=url
url=url,
search_title=search_title
)
return jsonify({
+11 -6
View File
@@ -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({
@@ -385,9 +390,9 @@ class SearchService:
return results
def save_to_articles(self, product_names, category, keywords, summary, content, source, url=None):
def save_to_articles(self, product_names, category, keywords, summary, content, source, url=None, search_title=None):
"""保存搜索结果到内容库"""
return db.add_article(product_names, category, keywords, summary, content, source, url)
return db.add_article(product_names, category, keywords, summary, content, source, url, search_title)
# 全局搜索服务实例
search_service = SearchService()
+13
View File
@@ -228,6 +228,19 @@
font-size: 12px;
}
.article-search-title {
font-size: 13px;
color: #666;
margin-bottom: 10px;
padding: 4px 8px;
background: #f0f4ff;
border-radius: 4px;
}
.article-search-title i {
color: #667eea;
}
.article-summary {
font-size: 14px;
color: #666;
+13
View File
@@ -268,6 +268,19 @@
color: #667eea;
}
.result-page-title {
font-size: 13px;
color: #666;
margin-bottom: 5px;
padding: 4px 8px;
background: #f0f4ff;
border-radius: 4px;
}
.result-page-title i {
color: #667eea;
}
.result-title .saved-icon {
color: #10b981;
}
+5
View File
@@ -118,6 +118,11 @@ function displayArticles() {
</button>
</div>
</div>
${article.search_title && article.search_title !== productNames.join(', ') ? `
<div class="article-search-title">
<i class="ri-search-line"></i> 搜索标题: ${escapeHtml(article.search_title)}
</div>
` : ''}
<div class="article-meta">
${article.category ? `<span class="article-category">${escapeHtml(article.category)}</span>` : ''}
<span><i class="ri-link"></i> ${escapeHtml(article.source || '未知来源')}</span>
+8 -2
View File
@@ -135,6 +135,11 @@ function displayResults() {
${r.saved ? '<i class="ri-check-line saved-icon"></i>' : ''}
${escapeHtml(r.title)}
</div>
${r.pageTitle && r.pageTitle !== r.title ? `
<div class="result-page-title">
<i class="ri-article-line"></i> 网页标题: ${escapeHtml(r.pageTitle)}
</div>
` : ''}
<div class="result-url">
<a href="${escapeHtml(r.url)}" target="_blank">
<i class="ri-external-link-line"></i>
@@ -207,7 +212,8 @@ async function fetchResult(index) {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
url: result.url,
product_names: [result.title]
product_names: [result.title],
search_title: result.title // 搜索结果的标题
})
});
@@ -216,7 +222,7 @@ async function fetchResult(index) {
if (data.success) {
searchResults[index].fetched = true;
searchResults[index].content = data.data.content;
searchResults[index].title = data.data.title || result.title;
searchResults[index].pageTitle = data.data.title || result.title; // 抓取到的标题
btn.innerHTML = '<i class="ri-check-line"></i> 已抓取';