Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
24f0d3c35b | ||
|
|
b0d98b78d9 | ||
|
|
d63fe2f671 | ||
|
|
c40acab1c9 |
+3
-2
@@ -114,7 +114,7 @@ def fetch_article():
|
||||
|
||||
result = search_service.fetch_url_content(url)
|
||||
|
||||
if result:
|
||||
if result and result.get('success'):
|
||||
# 自动保存到内容库
|
||||
article_id = search_service.save_to_articles(
|
||||
product_names=data.get('product_names', [result['title']]),
|
||||
@@ -132,7 +132,8 @@ def fetch_article():
|
||||
'data': result
|
||||
})
|
||||
else:
|
||||
return jsonify({'error': '抓取失败'}), 500
|
||||
error_msg = result.get('error', '抓取失败') if result else '抓取失败'
|
||||
return jsonify({'success': False, 'error': error_msg}), 500
|
||||
|
||||
@bp.route('/internet-search', methods=['POST'])
|
||||
def internet_search():
|
||||
|
||||
+60
-11
@@ -151,14 +151,14 @@ class SearchService:
|
||||
continue
|
||||
|
||||
# 匹配标题链接:link "标题文字" [ref=eXX]
|
||||
# 需要过滤域名链接(如 "zhihu.com")和短链接
|
||||
# 过滤域名链接(如 "zhihu.com")和短链接
|
||||
if 'link "' in line and '[ref=' in line:
|
||||
match = re.search(r'link "([^"]+)" \[ref=(e\d+)\]', line)
|
||||
if match:
|
||||
title = match.group(1)
|
||||
ref = match.group(2)
|
||||
# 过滤短标题(域名链接如 "zhihu.com")
|
||||
if len(title) > 20 and '.' not in title[:10]: # 不是域名格式
|
||||
# 放宽过滤条件:只要不是纯域名格式就保留
|
||||
if not (title.endswith('.com') or title.endswith('.cn') or title.endswith('.net')):
|
||||
refs.append((title, ref))
|
||||
|
||||
# 获取每个结果的 URL
|
||||
@@ -224,16 +224,22 @@ class SearchService:
|
||||
return None
|
||||
|
||||
def fetch_url_content(self, url):
|
||||
"""抓取网页内容(使用 agent-browser 浏览器方式,绑过反爬虫)"""
|
||||
"""抓取网页内容(使用 agent-browser 浏览器方式,绕过反爬虫)"""
|
||||
error_message = None
|
||||
try:
|
||||
# 使用浏览器方式抓取
|
||||
stdout, stderr, code = self._run_browser('open', url, '--timeout', '20000')
|
||||
# 使用浏览器方式抓取,增加超时时间到60秒
|
||||
stdout, stderr, code = self._run_browser('open', url, '--timeout', '60000')
|
||||
if code != 0:
|
||||
error_message = stderr.strip() if stderr else '浏览器打开页面失败'
|
||||
print(f"打开页面失败: {stderr}")
|
||||
return None
|
||||
# 浏览器失败,尝试使用 requests 备用方案
|
||||
result = self._fetch_with_requests(url)
|
||||
if result:
|
||||
return result
|
||||
return {'success': False, 'error': error_message}
|
||||
|
||||
# 等待页面加载
|
||||
self._run_browser('wait', '5000')
|
||||
# 等待页面加载(增加到10秒)
|
||||
self._run_browser('wait', '10000')
|
||||
|
||||
# 获取页面标题
|
||||
stdout, stderr, code = self._run_browser('get', 'title', '--timeout', '5000')
|
||||
@@ -262,6 +268,7 @@ class SearchService:
|
||||
description = text[:200].strip() if text else ''
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'title': title,
|
||||
'description': description,
|
||||
'content': text,
|
||||
@@ -269,13 +276,14 @@ class SearchService:
|
||||
'fetch_date': datetime.now().isoformat()
|
||||
}
|
||||
except Exception as e:
|
||||
print(f"抓取URL失败: {url}, 错误: {str(e)}")
|
||||
error_message = str(e)
|
||||
print(f"抓取URL失败: {url}, 错误: {error_message}")
|
||||
# 尝试关闭浏览器
|
||||
try:
|
||||
self._run_browser('close')
|
||||
except:
|
||||
pass
|
||||
return None
|
||||
return {'success': False, 'error': error_message}
|
||||
|
||||
def _extract_text_from_snapshot(self, snapshot):
|
||||
"""从 accessibility tree snapshot 中提取文本内容"""
|
||||
@@ -309,6 +317,47 @@ class SearchService:
|
||||
|
||||
return result
|
||||
|
||||
def _fetch_with_requests(self, url):
|
||||
"""备用方案:使用 requests 抓取静态内容"""
|
||||
try:
|
||||
headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8'
|
||||
}
|
||||
response = requests.get(url, headers=headers, timeout=30)
|
||||
response.raise_for_status()
|
||||
|
||||
soup = BeautifulSoup(response.text, 'html.parser')
|
||||
|
||||
# 获取标题
|
||||
title = soup.title.string.strip() if soup.title else ''
|
||||
|
||||
# 移除不需要的标签
|
||||
for tag in soup(['script', 'style', 'nav', 'footer', 'header', 'aside']):
|
||||
tag.decompose()
|
||||
|
||||
# 获取主要内容
|
||||
text = soup.get_text(separator='\n', strip=True)
|
||||
# 清理多余空白行
|
||||
lines = [line.strip() for line in text.split('\n') if line.strip()]
|
||||
text = '\n'.join(lines)
|
||||
|
||||
# 提取描述(前200字符)
|
||||
description = text[:200].strip() if text else ''
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'title': title,
|
||||
'description': description,
|
||||
'content': text,
|
||||
'url': url,
|
||||
'fetch_date': datetime.now().isoformat()
|
||||
}
|
||||
except Exception as e:
|
||||
print(f"备用抓取失败: {url}, 错误: {str(e)}")
|
||||
return None
|
||||
|
||||
def search_articles(self, keyword, category=None):
|
||||
"""从内容库搜索"""
|
||||
return db.search_articles(keyword, category)
|
||||
|
||||
@@ -151,6 +151,13 @@
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.progress-info {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
/* 结果区域 */
|
||||
.results-section {
|
||||
background: white;
|
||||
|
||||
+41
-5
@@ -5,6 +5,9 @@ const API_BASE = '';
|
||||
let searchResults = [];
|
||||
let currentResultIndex = -1;
|
||||
|
||||
// 自动流程控制
|
||||
let shouldStop = false;
|
||||
|
||||
// 页面加载初始化
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
// 回车搜索
|
||||
@@ -42,9 +45,11 @@ async function doSearch() {
|
||||
updateStatus('searching', `正在通过 ${engineNames[engine]} 搜索...`);
|
||||
document.getElementById('search-btn').disabled = true;
|
||||
|
||||
// 显示进度
|
||||
// 显示进度和停止按钮
|
||||
shouldStop = false;
|
||||
const progress = document.getElementById('search-progress');
|
||||
progress.style.display = 'block';
|
||||
document.getElementById('stop-btn').style.display = 'inline-flex';
|
||||
document.getElementById('progress-fill').style.width = '0%';
|
||||
document.getElementById('progress-text').textContent = '正在搜索...';
|
||||
|
||||
@@ -67,6 +72,14 @@ async function doSearch() {
|
||||
|
||||
displayResults();
|
||||
|
||||
// 检查是否被停止
|
||||
if (shouldStop) {
|
||||
document.getElementById('stop-btn').style.display = 'none';
|
||||
document.getElementById('progress-text').textContent = '已停止';
|
||||
updateStatus('warning', '搜索已停止');
|
||||
return;
|
||||
}
|
||||
|
||||
// 显示缓存状态
|
||||
if (data.cached) {
|
||||
updateStatus('success', `找到 ${searchResults.length} 条结果(使用缓存)`);
|
||||
@@ -80,25 +93,28 @@ async function doSearch() {
|
||||
|
||||
document.getElementById('progress-fill').style.width = '100%';
|
||||
document.getElementById('progress-text').textContent = '搜索完成!';
|
||||
document.getElementById('stop-btn').style.display = 'none'; // 隐藏停止按钮
|
||||
|
||||
// 自动抓取和保存
|
||||
if (autoFetch && searchResults.length > 0) {
|
||||
setTimeout(() => fetchAllResults(autoSave), 500);
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
progress.style.display = 'none';
|
||||
}, 1000);
|
||||
}
|
||||
} else {
|
||||
document.getElementById('stop-btn').style.display = 'none';
|
||||
updateStatus('error', '搜索失败');
|
||||
showToast('搜索失败: ' + data.error, 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
document.getElementById('stop-btn').style.display = 'none';
|
||||
updateStatus('error', '搜索出错');
|
||||
showToast('搜索出错', 'error');
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
progress.style.display = 'none';
|
||||
}, 1000);
|
||||
|
||||
document.getElementById('search-btn').disabled = false;
|
||||
}
|
||||
|
||||
@@ -338,13 +354,27 @@ async function saveAllResults() {
|
||||
|
||||
// 抓取所有结果
|
||||
async function fetchAllResults(autoSave) {
|
||||
shouldStop = false; // 重置停止标志
|
||||
|
||||
const progress = document.getElementById('search-progress');
|
||||
progress.style.display = 'block';
|
||||
document.getElementById('stop-btn').style.display = 'inline-flex'; // 显示停止按钮
|
||||
|
||||
const total = searchResults.length;
|
||||
let fetched = 0;
|
||||
|
||||
for (let i = 0; i < searchResults.length; i++) {
|
||||
// 检查是否停止
|
||||
if (shouldStop) {
|
||||
document.getElementById('progress-text').textContent = `已停止(已抓取 ${fetched}/${total})`;
|
||||
document.getElementById('stop-btn').style.display = 'none';
|
||||
setTimeout(() => {
|
||||
progress.style.display = 'none';
|
||||
}, 2000);
|
||||
showToast('自动抓取已停止', 'warning');
|
||||
return;
|
||||
}
|
||||
|
||||
if (searchResults[i].fetched) {
|
||||
fetched++;
|
||||
continue;
|
||||
@@ -361,6 +391,7 @@ async function fetchAllResults(autoSave) {
|
||||
|
||||
document.getElementById('progress-fill').style.width = '100%';
|
||||
document.getElementById('progress-text').textContent = '抓取完成!';
|
||||
document.getElementById('stop-btn').style.display = 'none';
|
||||
|
||||
setTimeout(() => {
|
||||
progress.style.display = 'none';
|
||||
@@ -372,6 +403,11 @@ async function fetchAllResults(autoSave) {
|
||||
}
|
||||
}
|
||||
|
||||
// 停止自动处理
|
||||
function stopAutoProcess() {
|
||||
shouldStop = true;
|
||||
}
|
||||
|
||||
// 显示结果详情
|
||||
function showResultDetail(index) {
|
||||
currentResultIndex = index;
|
||||
|
||||
@@ -62,7 +62,12 @@
|
||||
<div class="progress-bar">
|
||||
<div class="progress-fill" id="progress-fill"></div>
|
||||
</div>
|
||||
<div class="progress-text" id="progress-text">正在搜索...</div>
|
||||
<div class="progress-info">
|
||||
<span class="progress-text" id="progress-text">正在搜索...</span>
|
||||
<button onclick="stopAutoProcess()" class="btn btn-danger btn-sm" id="stop-btn" style="display: none;">
|
||||
<i class="ri-stop-line"></i> 停止
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 搜索结果 -->
|
||||
|
||||
Reference in New Issue
Block a user