互联网搜索支持多搜索引擎选择

- 新增搜索引擎选择下拉框
- 支持 Bing 中国(默认)、Bing 国际、Google、百度
- 后端 API 支持 engine 参数
- 添加百度搜索结果解析方法
This commit is contained in:
2026-07-13 17:03:26 +08:00
parent 9d6481bd06
commit c3eef4fa21
5 changed files with 95 additions and 12 deletions
+62 -8
View File
@@ -33,18 +33,32 @@ class SearchService:
)
return result.stdout, result.stderr, result.returncode
def search_internet(self, keyword, max_results=None):
def search_internet(self, keyword, max_results=None, engine='bing_cn'):
"""
从互联网搜索(使用 agent-browser 浏览器自动化)
支持的搜索引擎:
- bing_cn: Bing 中国(默认)
- bing_global: Bing 国际版
- google: Google
- baidu: 百度
"""
max_results = max_results or self.max_results
results = []
# 根据搜索引擎选择 URL
encoded_keyword = urllib.parse.quote(keyword)
search_urls = {
'bing_cn': f"https://cn.bing.com/search?q={encoded_keyword}",
'bing_global': f"https://www.bing.com/search?q={encoded_keyword}",
'google': f"https://www.google.com/search?q={encoded_keyword}",
'baidu': f"https://www.baidu.com/s?wd={encoded_keyword}"
}
search_url = search_urls.get(engine, search_urls['bing_cn'])
try:
# 1. 打开 Bing 搜索
encoded_keyword = urllib.parse.quote(keyword)
search_url = f"https://www.bing.com/search?q={encoded_keyword}"
# 1. 打开搜索引擎
stdout, stderr, code = self._run_browser('open', search_url, '--timeout', '20000')
if code != 0:
print(f"打开搜索页面失败: {stderr}")
@@ -66,9 +80,11 @@ class SearchService:
print(f"解析 JSON 失败: {stdout[:500]}")
return results
# 4. 从 accessibility tree 中提取搜索结果
# Bing 搜索结果在 main[aria-label="搜索结果"] 区域内
results = self._parse_bing_results(data, max_results)
# 4. 根据搜索引擎选择解析方法
if engine == 'baidu':
results = self._parse_baidu_results(data, max_results)
else:
results = self._parse_bing_results(data, max_results)
# 5. 关闭浏览器
self._run_browser('close')
@@ -143,6 +159,44 @@ class SearchService:
return results
def _parse_baidu_results(self, snapshot_data, max_results=10):
"""从百度搜索结果中解析标题和链接"""
results = []
snapshot = snapshot_data.get('data', {}).get('snapshot', '')
if not snapshot:
return results
# 百度搜索结果解析
refs = []
lines = snapshot.split('\n')
for line in lines:
line = line.strip()
# 百度结果通常在 link 标签中
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)
# 过滤百度内部链接和广告
if len(title) > 10 and '百度' not in title[:6]:
refs.append((title, ref))
# 获取每个结果的 URL
for title, ref in refs[:max_results]:
url = self._get_link_url(ref)
if url and 'baidu.com' not in url:
results.append({
'title': title,
'url': url,
'snippet': '',
'source': 'baidu'
})
return results
def _get_link_url(self, ref):
"""通过 agent-browser 获取链接的 URL"""
try: