argo-search 2.9.9 → 2.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +39 -22
- package/README.es.md +39 -22
- package/README.ja.md +38 -21
- package/README.ko.md +38 -21
- package/README.md +41 -22
- package/SKILL.md +13 -12
- package/backends/domain_profiles.json +76 -0
- package/backends/engine_registry.yaml +163 -7
- package/backends/quota_profiles.json +122 -2
- package/backends/source_types_cn.json +9 -0
- package/bin/argo +22 -1
- package/bin/argo.js +3 -3
- package/config.yaml +150 -30
- package/docs/ARGO_INTRO.md +4 -4
- package/docs/ENGINE_CATALOG.md +68 -48
- package/docs/MCP_SETUP.md +1 -1
- package/docs/RELEASE_NOTES_v2.10.0.md +85 -0
- package/docs/RELEASE_NOTES_v2.10.1.md +57 -0
- package/docs/RELEASE_NOTES_v2.9.8.md +1 -1
- package/docs//344/270/272/344/273/200/344/271/210/351/200/211/346/213/251argo.md +9 -9
- package/engines/_template_http.yaml +2 -0
- package/engines/specs/clawhub.yaml +16 -15
- package/engines/specs/deepseek1024.yaml +46 -0
- package/engines/specs/dsh_hub.yaml +44 -0
- package/engines/specs/dshopen.yaml +32 -0
- package/engines/specs/explainx.yaml +32 -0
- package/engines/specs/gbfs_nyc.yaml +6 -1
- package/engines/specs/metaso_doc.yaml +49 -0
- package/engines/specs/metaso_image.yaml +65 -0
- package/engines/specs/metaso_podcast.yaml +60 -0
- package/engines/specs/metaso_scholar.yaml +57 -0
- package/engines/specs/metaso_video.yaml +62 -0
- package/engines/specs/ncpssd.yaml +44 -0
- package/engines/specs/sciverse.yaml +73 -0
- package/engines/specs/skills_sh.yaml +32 -0
- package/engines/specs/skillsmp.yaml +18 -16
- package/package.json +4 -2
- package/packages/dsh-plugin/README.md +3 -2
- package/packages/dsh-plugin/package.json +1 -1
- package/packages/dsh-plugin/test/size-gate.test.js +113 -0
- package/scripts/adaptive.py +41 -14
- package/scripts/article.py +6 -1
- package/scripts/article_meta.py +151 -2
- package/scripts/bench_general_pool.py +145 -0
- package/scripts/candidate_envelope.py +45 -4
- package/scripts/charset_decode.py +127 -0
- package/scripts/ci_drift_gate.py +47 -24
- package/scripts/crawl.py +240 -74
- package/scripts/crawl_frontier.py +217 -0
- package/scripts/crawl_policy.py +174 -0
- package/scripts/engine_dispatch.py +23 -18
- package/scripts/engine_failure.py +15 -0
- package/scripts/engine_status.py +12 -5
- package/scripts/engine_validate.py +8 -0
- package/scripts/engines.py +15 -0
- package/scripts/engines_base.py +43 -15
- package/scripts/engines_builders.py +14 -0
- package/scripts/engines_builders_data.py +10 -3
- package/scripts/engines_builders_dsh.py +330 -0
- package/scripts/engines_builders_skill.py +314 -0
- package/scripts/evidence.py +26 -5
- package/scripts/fetch_v3.py +37 -5
- package/scripts/gen_engine_catalog.py +218 -2
- package/scripts/general_pool.py +162 -0
- package/scripts/http_client.py +38 -42
- package/scripts/image_ops.py +4 -0
- package/scripts/install.ps1 +47 -19
- package/scripts/install.sh +45 -20
- package/scripts/local_seek.py +53 -11
- package/scripts/matrix_search_eval.py +17 -1
- package/scripts/mcp_transport.py +1 -1
- package/scripts/ncpssd_bridge.py +123 -0
- package/scripts/paid_policy.py +109 -13
- package/scripts/pre-commit +19 -4
- package/scripts/query_signals.py +156 -3
- package/scripts/redskill/redskill_engine.py +29 -2
- package/scripts/release.py +398 -0
- package/scripts/release_check.py +209 -0
- package/scripts/retry_policy.py +22 -1
- package/scripts/robots_guard.py +26 -0
- package/scripts/route.py +11 -8
- package/scripts/route_cache.py +6 -0
- package/scripts/route_combo.py +37 -59
- package/scripts/route_domains.py +22 -3
- package/scripts/route_policy.py +6 -0
- package/scripts/search.py +13 -3
- package/scripts/search_cli.py +49 -0
- package/scripts/search_entry.py +9 -0
- package/scripts/search_output.py +43 -0
- package/scripts/search_pipeline.py +43 -3
- package/scripts/search_rank.py +4 -1
- package/scripts/serp_guard.py +11 -0
- package/scripts/social_engines/bilibili_engine.py +163 -26
- package/scripts/social_engines/reddit_engine.py +52 -51
- package/scripts/source_selection.py +302 -0
- package/scripts/url_canon.py +2 -1
- package/scripts/video_fetch.py +303 -0
- package/scripts/video_meta.py +225 -0
- package/scripts/video_route.py +65 -0
- package/scripts/watch.py +123 -0
- package/sub-skills/local-seek/scripts/seek.py +19 -4
- package/engines/specs/_cisa_kev.yaml +0 -54
- package/engines/specs/_osv.yaml +0 -63
- package/scripts/local_engines.py +0 -321
package/scripts/ci_drift_gate.py
CHANGED
|
@@ -1,25 +1,35 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
-
"""
|
|
2
|
+
"""漂移门判定:把夜间「重录 vs 滚动基线」的比较结果变成退出码。
|
|
3
3
|
|
|
4
4
|
## 为什么单独成文件
|
|
5
5
|
|
|
6
6
|
判定逻辑原来写在 nightly.yml 的 heredoc 里:语法对错只能在推上去之后知道,
|
|
7
7
|
阈值改松改紧也没人测。抽出来后有两个好处——本地能跑同一套判定,用例能锁住
|
|
8
|
-
|
|
8
|
+
「什么算回归」这条契约(tests/test_ci_drift_gate.py)。
|
|
9
9
|
|
|
10
|
-
##
|
|
10
|
+
## 2026-10-29 起只判结构回归,不再判数值漂移
|
|
11
11
|
|
|
12
|
-
-
|
|
13
|
-
|
|
12
|
+
活网重录下「结果换血」是常态不是回归:2026-10-28 夜(issue #35)四条固定
|
|
13
|
+
查询三条整批换血,asyncio 一条体积 +32% 破线,逐条核对全是外部排名漂移
|
|
14
|
+
(docs.python.org 掉出、内容农场页换入),没有一条是 argo 自己的问题。
|
|
15
|
+
静态基线 + 数值阈值的设计每周都会红一次、开一个无人能修的 issue。所以:
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
- **基线改滚动**:绿跑后由 nightly 把本次报告提交为新基线([skip ci]),
|
|
18
|
+
门比较的是「今晚 vs 昨晚」,源一夜之间死掉才红;
|
|
19
|
+
- **只判结构回归**:条数跌破地板(-1 允许)、单条查询零结果、基线里有的
|
|
20
|
+
用例整条消失(查询集被改或录制失败);
|
|
21
|
+
- **体积只记录不判**:超阈值打一行信息项进日志,供人翻 artifact 时对照,
|
|
22
|
+
不再当失败。
|
|
23
|
+
|
|
24
|
+
两处已知边界,刻意接受:每晚 -1 条的缓慢衰减不会红(地板只看单晚),
|
|
25
|
+
趋势由 ledger 账本的结果率曲线盯着;`not_recorded`(重放时要了一个当时
|
|
26
|
+
没调过的引擎)也不计入——录制只保留真正被调到的引擎,而重放是瞬时完成,
|
|
27
|
+
早停时机与真跑不同,这是录制机制的正常边界,当成回归报会制造长期噪音。
|
|
18
28
|
|
|
19
29
|
用法:
|
|
20
30
|
python3 scripts/ci_drift_gate.py --baseline 基线报告.json --current 本次报告.json
|
|
21
|
-
退出码:0 通过;1
|
|
22
|
-
|
|
31
|
+
退出码:0 通过;1 结构回归;2 输入缺失/格式不对(这两件事必须分开,混在
|
|
32
|
+
一起会让人以为「红了=质量掉了」)。
|
|
23
33
|
"""
|
|
24
34
|
|
|
25
35
|
from __future__ import annotations
|
|
@@ -30,8 +40,8 @@ import sys
|
|
|
30
40
|
from pathlib import Path
|
|
31
41
|
from typing import Any
|
|
32
42
|
|
|
33
|
-
KEPT_SLACK = 1 # 允许少 1
|
|
34
|
-
BYTES_GROWTH = 1.2 #
|
|
43
|
+
KEPT_SLACK = 1 # 允许少 1 条(工具源单条抖动很常见)
|
|
44
|
+
BYTES_GROWTH = 1.2 # 体积信息项:超过基线 20% 打一行,不判失败
|
|
35
45
|
|
|
36
46
|
|
|
37
47
|
def _load(path: Path) -> dict[str, Any]:
|
|
@@ -45,17 +55,14 @@ def violations(base: dict[str, Any], cur: dict[str, Any]) -> list[str]:
|
|
|
45
55
|
cur_ids = {c.get("id") for c in (cur.get("cases") or [])}
|
|
46
56
|
out: list[str] = []
|
|
47
57
|
for case in cur.get("cases") or []:
|
|
48
|
-
old = by_id.get(case.get("id"))
|
|
49
|
-
if not old:
|
|
50
|
-
continue # 新增的用例没有基线,不算漂移
|
|
51
58
|
cid = case.get("id")
|
|
52
|
-
kept
|
|
53
|
-
if kept
|
|
54
|
-
out.append(f"{cid}:
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
+
kept = case.get("kept", 0)
|
|
60
|
+
if kept == 0:
|
|
61
|
+
out.append(f"{cid}: 零结果——交付层死了(源挂/解析坏),这不是查询没人问")
|
|
62
|
+
continue
|
|
63
|
+
old = by_id.get(cid)
|
|
64
|
+
if old and kept < old.get("kept", 0) - KEPT_SLACK:
|
|
65
|
+
out.append(f"{cid}: 结果 {kept} 条,基线 {old.get('kept', 0)} 条")
|
|
59
66
|
# 基线里有、本次没有:不是「通过」,是这条根本没被录到(查询集被改、
|
|
60
67
|
# 或录制整条失败)。2026-10-22 本地实跑踩过:只剩 1 条时判定照样退 0。
|
|
61
68
|
for cid in by_id:
|
|
@@ -64,8 +71,22 @@ def violations(base: dict[str, Any], cur: dict[str, Any]) -> list[str]:
|
|
|
64
71
|
return out
|
|
65
72
|
|
|
66
73
|
|
|
74
|
+
def drift_notes(base: dict[str, Any], cur: dict[str, Any]) -> list[str]:
|
|
75
|
+
"""信息项:写进日志供人对照,不参与退出码。"""
|
|
76
|
+
by_id = {c.get("id"): c for c in (base.get("cases") or [])}
|
|
77
|
+
notes: list[str] = []
|
|
78
|
+
for case in cur.get("cases") or []:
|
|
79
|
+
old_bytes = (by_id.get(case.get("id")) or {}).get("bytes", 0)
|
|
80
|
+
new_bytes = case.get("bytes", 0)
|
|
81
|
+
if old_bytes and new_bytes > old_bytes * BYTES_GROWTH:
|
|
82
|
+
pct = round((new_bytes - old_bytes) / old_bytes * 100)
|
|
83
|
+
notes.append(f"{case.get('id')}: 输出 {new_bytes}B,基线 {old_bytes}B"
|
|
84
|
+
f"(+{pct}%)——网页常态漂移,仅记录")
|
|
85
|
+
return notes
|
|
86
|
+
|
|
87
|
+
|
|
67
88
|
def main() -> int:
|
|
68
|
-
ap = argparse.ArgumentParser(description="
|
|
89
|
+
ap = argparse.ArgumentParser(description="夜间漂移阈值判定(只判结构回归)")
|
|
69
90
|
ap.add_argument("--baseline", required=True)
|
|
70
91
|
ap.add_argument("--current", required=True)
|
|
71
92
|
args = ap.parse_args()
|
|
@@ -77,12 +98,14 @@ def main() -> int:
|
|
|
77
98
|
print(f"读不到报告:{type(e).__name__}: {e}", file=sys.stderr)
|
|
78
99
|
return 2
|
|
79
100
|
|
|
101
|
+
for note in drift_notes(base, cur):
|
|
102
|
+
print(f"信息项:{note}")
|
|
80
103
|
bad = violations(base, cur)
|
|
81
104
|
if bad:
|
|
82
105
|
print("\n".join(bad), file=sys.stderr)
|
|
83
106
|
return 1
|
|
84
107
|
print(f"通过:{len(cur.get('cases') or [])} 条,漏录 "
|
|
85
|
-
f"{cur.get('total_missing', 0)}
|
|
108
|
+
f"{cur.get('total_missing', 0)} 个引擎(信息项,不算回归)")
|
|
86
109
|
return 0
|
|
87
110
|
|
|
88
111
|
|
package/scripts/crawl.py
CHANGED
|
@@ -1,13 +1,41 @@
|
|
|
1
1
|
#!/usr/bin/env python3
|
|
2
|
-
"""crawl.py — 站点级爬取(fetch_v3 降级链:增强 HTTP → TLS 指纹 → Wayback
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
"""crawl.py — 站点级爬取(fetch_v3 降级链:增强 HTTP → TLS 指纹 → Wayback)。
|
|
3
|
+
|
|
4
|
+
与旧版的区别在「抓什么」和「怎么给」两处:
|
|
5
|
+
|
|
6
|
+
抓什么:URL 先过 crawl_frontier(归一化、资产过滤、坏路径、范围),正文哈希去重;
|
|
7
|
+
每次请求过 crawl_policy(每主机间隔/并发、429 退避),robots 合规仍由
|
|
8
|
+
fetch_v3 内部的 robots_guard 负责。
|
|
9
|
+
怎么给:默认每页正文按 --max-chars 截断,--query 用现成的 BM25 聚焦只回相关段落,
|
|
10
|
+
--full 回全文。旧版固定 content[:500]、每页只跟进前 5 个链接,这两处
|
|
11
|
+
是「抓到了但没法用」的主因。
|
|
12
|
+
|
|
13
|
+
批量场景禁浏览器降级:BFS 可能爬几十页,每页启动 Chrome CDP 会拖慢整体。
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import hashlib
|
|
19
|
+
import re
|
|
20
|
+
import time
|
|
21
|
+
from collections import deque
|
|
22
|
+
|
|
23
|
+
from bounded_run import TaskError, run_bounded
|
|
6
24
|
from cli_io import dumps
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
25
|
+
from crawl_frontier import (
|
|
26
|
+
Frontier,
|
|
27
|
+
canonical,
|
|
28
|
+
extract_links,
|
|
29
|
+
looks_html,
|
|
30
|
+
strip_session_params,
|
|
31
|
+
)
|
|
32
|
+
from crawl_policy import HostGate, retry_after_seconds, status_of
|
|
33
|
+
|
|
34
|
+
# 单页抓取额度:需要完整 HTML 才能提取全部链接。fetch_v3 的 html 交付视图是
|
|
35
|
+
# max_chars*2,旧版用 2000 当抓取额度等于只看前 4KB,正文中后段的链接全丢。
|
|
36
|
+
_FETCH_CHARS = 200000
|
|
37
|
+
_PER_PAGE_LINKS = 200
|
|
38
|
+
_DEFAULT_MAX_CHARS = 2000
|
|
11
39
|
|
|
12
40
|
|
|
13
41
|
def _crawl_fetch(url: str, max_chars: int, timeout: int, raw: bool = True) -> dict:
|
|
@@ -17,85 +45,218 @@ def _crawl_fetch(url: str, max_chars: int, timeout: int, raw: bool = True) -> di
|
|
|
17
45
|
必须绕过缓存才能拿到完整页面。通过模块级 fetch_page 转发,
|
|
18
46
|
测试可 monkeypatch crawl.fetch_page 注入假抓取。
|
|
19
47
|
"""
|
|
20
|
-
|
|
48
|
+
from fetch_v3 import fetch_v3
|
|
49
|
+
return fetch_v3(url, max_chars=max_chars, timeout=float(timeout),
|
|
50
|
+
use_browser_fallback=False, skip_cache=raw, need_html=raw)
|
|
21
51
|
|
|
22
52
|
|
|
23
|
-
# 默认实现指向 fetch_v3
|
|
24
|
-
|
|
53
|
+
# 默认实现指向 fetch_v3 的兼容入口;测试可整体替换 fetch_page,
|
|
54
|
+
# 也可以按旧契约替换 _crawl_fetch(这里保留一层转发,两种打桩都生效)。
|
|
55
|
+
def fetch_page(url: str, max_chars: int, timeout: int, raw: bool = True) -> dict:
|
|
56
|
+
return _crawl_fetch(url, max_chars, timeout, raw=raw)
|
|
25
57
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
58
|
+
|
|
59
|
+
def _content_hash(text: str) -> str:
|
|
60
|
+
norm = re.sub(r"\s+", " ", text or "").strip()
|
|
61
|
+
return hashlib.sha256(norm.encode("utf-8")).hexdigest()[:16]
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _focus(content: str, query: str) -> str:
|
|
65
|
+
if not query or not content:
|
|
66
|
+
return content
|
|
67
|
+
try:
|
|
68
|
+
from focus_extract import focus_extract
|
|
69
|
+
return focus_extract(content, query, top_k=5)
|
|
70
|
+
except Exception:
|
|
71
|
+
return content
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _shape_page(result: dict, url: str, depth: int, max_chars: int,
|
|
75
|
+
query: str) -> dict | None:
|
|
76
|
+
content = result.get("content") or ""
|
|
77
|
+
if not content:
|
|
78
|
+
return None
|
|
79
|
+
focused = _focus(content, query)
|
|
80
|
+
delivered = focused if max_chars <= 0 else focused[:max_chars]
|
|
81
|
+
return {
|
|
82
|
+
"url": url,
|
|
83
|
+
"depth": depth,
|
|
84
|
+
"title": str(result.get("title") or "")[:200],
|
|
85
|
+
"content": delivered,
|
|
86
|
+
"length": len(focused),
|
|
87
|
+
"hash": _content_hash(content),
|
|
88
|
+
"status": status_of(result),
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _fetch_batch(batch: list[tuple[str, int]], timeout: int, gate: HostGate,
|
|
93
|
+
stats: dict) -> list[tuple[str, int, dict]]:
|
|
94
|
+
"""并发抓一层;gate 在 worker 内部阻塞,所以等待预算要算上排队时间。"""
|
|
95
|
+
interval = gate.interval
|
|
96
|
+
|
|
97
|
+
def worker(item: tuple[str, int]) -> dict:
|
|
98
|
+
url, _depth = item
|
|
99
|
+
with gate.slot(url) as lease:
|
|
100
|
+
try:
|
|
101
|
+
result = fetch_page(url, _FETCH_CHARS, timeout, raw=True)
|
|
102
|
+
except Exception as exc: # noqa: BLE001 - 单页失败不拖垮整次爬取
|
|
103
|
+
lease.note(0)
|
|
104
|
+
stats["fetch_failed"] += 1
|
|
105
|
+
return {"success": False, "error": str(exc)[:160]}
|
|
106
|
+
lease.note(status_of(result), retry_after_seconds(result))
|
|
107
|
+
return result
|
|
108
|
+
|
|
109
|
+
finished, unfinished = run_bounded(
|
|
110
|
+
batch, worker,
|
|
111
|
+
wait_s=float(timeout) * 2 + interval * max(1, len(batch)) + 5,
|
|
112
|
+
max_workers=min(len(batch), 8),
|
|
113
|
+
)
|
|
114
|
+
stats["unfinished_urls"] += len(unfinished)
|
|
115
|
+
out = []
|
|
116
|
+
for (url, depth), result in finished:
|
|
117
|
+
if isinstance(result, TaskError) or not isinstance(result, dict):
|
|
118
|
+
stats["fetch_failed"] += 1
|
|
53
119
|
continue
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
120
|
+
out.append((url, depth, result))
|
|
121
|
+
# 按入队顺序返回,不按线程完成先后:同一份输入两次爬取要得到同样的页序,
|
|
122
|
+
# 否则测试和调用方的 diff 都会被调度抖动影响。
|
|
123
|
+
order = {url: index for index, (url, _depth) in enumerate(batch)}
|
|
124
|
+
out.sort(key=lambda item: order.get(item[0], len(order)))
|
|
125
|
+
return out
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _crawl(url: str, max_pages: int, max_depth: int, timeout: int,
|
|
129
|
+
strategy: str, query: str = "", scope: str = "auto",
|
|
130
|
+
max_chars: int = _DEFAULT_MAX_CHARS, interval: float = 1.0,
|
|
131
|
+
policy: HostGate | None = None) -> dict:
|
|
132
|
+
t0 = time.monotonic()
|
|
133
|
+
frontier = Frontier(url, scope=scope)
|
|
134
|
+
gate = policy or HostGate(interval=interval)
|
|
135
|
+
stats = {key: 0 for key in (
|
|
136
|
+
"offsite", "asset", "pathological", "out_of_scope", "non_html",
|
|
137
|
+
"duplicate", "fetch_failed", "unfinished_urls")}
|
|
138
|
+
pages: list[dict] = []
|
|
139
|
+
seen_hashes: set[str] = set()
|
|
140
|
+
visited: set[str] = set()
|
|
141
|
+
|
|
142
|
+
if strategy == "sitemap":
|
|
143
|
+
seeds = _sitemap_seeds(url, timeout, gate, stats)
|
|
144
|
+
queue = deque((seed, 0) for seed in seeds[:max_pages * 2])
|
|
145
|
+
else:
|
|
146
|
+
queue = deque([(strip_session_params(canonical(url)), 0)])
|
|
147
|
+
|
|
63
148
|
while queue and len(pages) < max_pages:
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
149
|
+
batch: list[tuple[str, int]] = []
|
|
150
|
+
while queue and len(batch) < max_pages - len(pages):
|
|
151
|
+
candidate, depth = queue.popleft()
|
|
152
|
+
if depth > max_depth:
|
|
153
|
+
continue
|
|
154
|
+
ok, reason = frontier.accept(candidate)
|
|
155
|
+
if not ok:
|
|
156
|
+
stats[reason] += 1
|
|
69
157
|
continue
|
|
70
|
-
visited
|
|
71
|
-
|
|
72
|
-
|
|
158
|
+
if candidate in visited:
|
|
159
|
+
continue
|
|
160
|
+
visited.add(candidate)
|
|
161
|
+
batch.append((candidate, depth))
|
|
162
|
+
if not batch:
|
|
73
163
|
break
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if
|
|
164
|
+
for candidate, depth, result in _fetch_batch(batch, timeout, gate, stats):
|
|
165
|
+
if not result.get("success"):
|
|
166
|
+
stats["fetch_failed"] += 1
|
|
167
|
+
continue
|
|
168
|
+
html = result.get("html") or ""
|
|
169
|
+
if html and not looks_html(html):
|
|
170
|
+
stats["non_html"] += 1
|
|
80
171
|
continue
|
|
81
|
-
|
|
172
|
+
page = _shape_page(result, candidate, depth, max_chars, query)
|
|
173
|
+
if page is None:
|
|
174
|
+
stats["non_html"] += 1
|
|
175
|
+
continue
|
|
176
|
+
if page["hash"] in seen_hashes:
|
|
177
|
+
stats["duplicate"] += 1
|
|
178
|
+
else:
|
|
179
|
+
seen_hashes.add(page["hash"])
|
|
180
|
+
pages.append(page)
|
|
82
181
|
if len(pages) >= max_pages:
|
|
83
182
|
break
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
queue.append((
|
|
91
|
-
return {
|
|
92
|
-
|
|
183
|
+
if depth >= max_depth:
|
|
184
|
+
continue
|
|
185
|
+
# 正文重复的页面仍要跟进链接:列表页/分页常常正文相同但链接不同。
|
|
186
|
+
for link in extract_links(html, candidate)[:_PER_PAGE_LINKS]:
|
|
187
|
+
link = strip_session_params(link)
|
|
188
|
+
if link not in visited:
|
|
189
|
+
queue.append((link, depth + 1))
|
|
190
|
+
return {
|
|
191
|
+
"url": url,
|
|
192
|
+
"pages": pages,
|
|
193
|
+
"total": len(pages),
|
|
194
|
+
"skipped": stats,
|
|
195
|
+
"elapsed_ms": int((time.monotonic() - t0) * 1000),
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def _sitemap_seeds(url: str, timeout: int, gate: HostGate, stats: dict) -> list[str]:
|
|
200
|
+
"""取 sitemap 里的 <loc>,过前沿规则后返回;取不到退回空列表。"""
|
|
201
|
+
sitemap_url = url.rstrip("/") + "/sitemap.xml"
|
|
202
|
+
try:
|
|
203
|
+
with gate.slot(sitemap_url) as lease:
|
|
204
|
+
result = fetch_page(sitemap_url, _FETCH_CHARS, timeout, raw=True)
|
|
205
|
+
lease.note(status_of(result), retry_after_seconds(result))
|
|
206
|
+
except Exception:
|
|
207
|
+
return []
|
|
208
|
+
if not result.get("success"):
|
|
209
|
+
return []
|
|
210
|
+
text = result.get("html") or ""
|
|
211
|
+
locs = re.findall(r"<loc>\s*(.*?)\s*</loc>", text, flags=re.IGNORECASE | re.DOTALL)
|
|
212
|
+
# sitemap 天然列全站,范围按整个主机算,不套起始路径前缀。
|
|
213
|
+
frontier = Frontier(url, scope="domain")
|
|
214
|
+
seeds = []
|
|
215
|
+
for loc in locs:
|
|
216
|
+
loc = strip_session_params(canonical(loc))
|
|
217
|
+
ok, reason = frontier.accept(loc)
|
|
218
|
+
if not ok:
|
|
219
|
+
stats[reason] += 1
|
|
220
|
+
continue
|
|
221
|
+
seeds.append(loc)
|
|
222
|
+
return seeds
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def crawl_sitemap(url: str, max_pages: int = 20, timeout: int = 10,
|
|
226
|
+
query: str = "", scope: str = "auto",
|
|
227
|
+
max_chars: int = _DEFAULT_MAX_CHARS, interval: float = 1.0,
|
|
228
|
+
policy: HostGate | None = None) -> dict:
|
|
229
|
+
return _crawl(url, max_pages, 0, timeout, "sitemap", query=query,
|
|
230
|
+
scope=scope, max_chars=max_chars, interval=interval,
|
|
231
|
+
policy=policy)
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def crawl_bfs(url: str, max_pages: int = 10, max_depth: int = 2,
|
|
235
|
+
timeout: int = 8, query: str = "", scope: str = "auto",
|
|
236
|
+
max_chars: int = _DEFAULT_MAX_CHARS, interval: float = 1.0,
|
|
237
|
+
policy: HostGate | None = None) -> dict:
|
|
238
|
+
return _crawl(url, max_pages, max_depth, timeout, "bfs", query=query,
|
|
239
|
+
scope=scope, max_chars=max_chars, interval=interval,
|
|
240
|
+
policy=policy)
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
if __name__ == "__main__":
|
|
93
244
|
import argparse
|
|
94
245
|
p = argparse.ArgumentParser()
|
|
95
246
|
p.add_argument('url')
|
|
96
|
-
p.add_argument('--strategy', default='bfs', choices=['sitemap','bfs'])
|
|
247
|
+
p.add_argument('--strategy', default='bfs', choices=['sitemap', 'bfs'])
|
|
97
248
|
p.add_argument('--max-pages', type=int, default=10)
|
|
98
249
|
p.add_argument('--max-depth', type=int, default=2)
|
|
250
|
+
p.add_argument('--query', default='',
|
|
251
|
+
help='BM25 聚焦:每页只回与查询相关的段落')
|
|
252
|
+
p.add_argument('--scope', default='auto',
|
|
253
|
+
choices=['auto', 'path', 'domain'],
|
|
254
|
+
help='auto=起始目录(叶子页上提一层)/ path=严格前缀 / domain=整站')
|
|
255
|
+
p.add_argument('--max-chars', type=int, default=_DEFAULT_MAX_CHARS,
|
|
256
|
+
help='每页交付字符数,0=不截断')
|
|
257
|
+
p.add_argument('--full', action='store_true', help='等价于 --max-chars 0')
|
|
258
|
+
p.add_argument('--interval', type=float, default=1.0,
|
|
259
|
+
help='同一主机两次请求的最小间隔(秒),默认 1')
|
|
99
260
|
# bin/argo 的 usage 承诺了 Common flags「--json」,此前 crawl/extract 的
|
|
100
261
|
# parser 里没有它,Agent 照文档传参直接撞墙(error: unrecognized
|
|
101
262
|
# arguments,exit 2)。这里接住该开关并给出真实语义:默认仍是截断预览
|
|
@@ -104,9 +265,14 @@ if __name__ == '__main__':
|
|
|
104
265
|
p.add_argument('--json', action='store_true',
|
|
105
266
|
help='输出完整 JSON(默认为 2000 字符截断预览)')
|
|
106
267
|
args = p.parse_args()
|
|
268
|
+
max_chars = 0 if args.full else args.max_chars
|
|
107
269
|
if args.strategy == 'sitemap':
|
|
108
|
-
r = crawl_sitemap(args.url, args.max_pages
|
|
270
|
+
r = crawl_sitemap(args.url, args.max_pages, query=args.query,
|
|
271
|
+
scope=args.scope, max_chars=max_chars,
|
|
272
|
+
interval=args.interval)
|
|
109
273
|
else:
|
|
110
|
-
r = crawl_bfs(args.url, args.max_pages, args.max_depth
|
|
274
|
+
r = crawl_bfs(args.url, args.max_pages, args.max_depth,
|
|
275
|
+
query=args.query, scope=args.scope, max_chars=max_chars,
|
|
276
|
+
interval=args.interval)
|
|
111
277
|
out = dumps(r)
|
|
112
278
|
print(out if args.json else out[:2000])
|