Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
909e8e01b5 | ||
|
|
e749d70a43 |
@@ -4,3 +4,4 @@ data/*.json
|
|||||||
data/cookies_*.json
|
data/cookies_*.json
|
||||||
logs/
|
logs/
|
||||||
out/
|
out/
|
||||||
|
data/exports/
|
||||||
@@ -104,13 +104,16 @@ def persist_cb(task_id, run):
|
|||||||
print(f"[db] 同步失败: {e}", flush=True)
|
print(f"[db] 同步失败: {e}", flush=True)
|
||||||
|
|
||||||
|
|
||||||
def start_run(task):
|
def start_run(task, skip_seed=False):
|
||||||
"""为任务启动一次爬取, 返回 (run, error)"""
|
"""为任务启动一次爬取, 返回 (run, error)
|
||||||
|
skip_seed: auto 任务继续爬取模式, 跳过起始网址直接从缓存队列消费
|
||||||
|
"""
|
||||||
with JOBS_LOCK:
|
with JOBS_LOCK:
|
||||||
job = JOBS.get(task["id"])
|
job = JOBS.get(task["id"])
|
||||||
if job and job.is_running():
|
if job and job.is_running():
|
||||||
return None, "该任务已有正在运行的爬取"
|
return None, "该任务已有正在运行的爬取"
|
||||||
run = make_run(task)
|
run = make_run(task)
|
||||||
|
run["skip_seed"] = bool(skip_seed)
|
||||||
store.add_run(task["id"], run)
|
store.add_run(task["id"], run)
|
||||||
job = CrawlJob(task, run, persist_cb)
|
job = CrawlJob(task, run, persist_cb)
|
||||||
JOBS[task["id"]] = job
|
JOBS[task["id"]] = job
|
||||||
@@ -424,6 +427,23 @@ def api_trash_clear():
|
|||||||
|
|
||||||
# ---------------- API: 运行控制 ----------------
|
# ---------------- API: 运行控制 ----------------
|
||||||
|
|
||||||
|
@app.route("/api/tasks/<tid>/continue", methods=["POST"])
|
||||||
|
def api_continue(tid):
|
||||||
|
"""继续爬取: auto 任务从待爬缓存队列接着爬 (跳过起始网址, 保留已爬集合)"""
|
||||||
|
task = store.get_task(tid)
|
||||||
|
if not task:
|
||||||
|
return jsonify({"error": "任务不存在"}), 404
|
||||||
|
if task.get("mode") != "auto":
|
||||||
|
return jsonify({"error": "仅自动爬取任务支持继续爬取"}), 400
|
||||||
|
st = store.load_auto_state(tid, task)
|
||||||
|
if not st.get("pending"):
|
||||||
|
return jsonify({"error": "没有待爬缓存链接,无需继续"}), 400
|
||||||
|
run, err = start_run(task, skip_seed=True)
|
||||||
|
if err:
|
||||||
|
return jsonify({"error": err}), 409
|
||||||
|
return jsonify(run)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/tasks/<tid>/start", methods=["POST"])
|
@app.route("/api/tasks/<tid>/start", methods=["POST"])
|
||||||
def api_start(tid):
|
def api_start(tid):
|
||||||
task = store.get_task(tid)
|
task = store.get_task(tid)
|
||||||
|
|||||||
+501
-496
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"pending": [],
|
||||||
|
"visited": [
|
||||||
|
"http://127.0.0.1:38081/p2",
|
||||||
|
"http://127.0.0.1:38081/p5",
|
||||||
|
"http://127.0.0.1:38081/p3",
|
||||||
|
"http://127.0.0.1:38081/",
|
||||||
|
"http://127.0.0.1:38081/p1",
|
||||||
|
"http://127.0.0.1:38081/p4"
|
||||||
|
]
|
||||||
|
}
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -598,9 +598,10 @@ class CrawlJob:
|
|||||||
def _crawl_auto(self):
|
def _crawl_auto(self):
|
||||||
"""自动爬取: 持续递归 爬取->发现链接->爬取... 直到无新链接可爬或达到上限
|
"""自动爬取: 持续递归 爬取->发现链接->爬取... 直到无新链接可爬或达到上限
|
||||||
- max_depth: 0=无限制, N=只爬 N 层
|
- max_depth: 0=无限制, N=只爬 N 层
|
||||||
- max_pages: 0=无限制, N=安全上限
|
- max_pages: 0=无限制, N=安全上限 (继续爬取模式按本次新增页数重新计算)
|
||||||
- 提取但未爬取的链接持久化到 auto.pending, 已爬集合持久化到 auto.visited
|
- 提取但未爬取的链接持久化到 auto.pending, 已爬集合持久化到 auto.visited
|
||||||
- 停止后再次运行从缓存队列继续爬; 起始网址每次运行都重新爬(不去重)
|
- 停止后再次运行从缓存队列继续爬; 起始网址每次运行都重新爬(不去重)
|
||||||
|
- skip_seed(继续爬取): 跳过起始网址直接消费缓存队列, 页数上限按本次新增重新计算
|
||||||
"""
|
"""
|
||||||
run = self.run
|
run = self.run
|
||||||
auto = self.task.get("auto", {})
|
auto = self.task.get("auto", {})
|
||||||
@@ -615,8 +616,13 @@ class CrawlJob:
|
|||||||
state = store.load_auto_state(self.task["id"], self.task)
|
state = store.load_auto_state(self.task["id"], self.task)
|
||||||
visited = set(state.get("visited", []) or [])
|
visited = set(state.get("visited", []) or [])
|
||||||
pending = state.get("pending", []) or []
|
pending = state.get("pending", []) or []
|
||||||
|
visited_base = len(visited)
|
||||||
# 起始网址每次运行都爬(不做去重), 缓存队列继续消费
|
# 起始网址每次运行都爬(不做去重), 缓存队列继续消费
|
||||||
queue = [(seed, 0, "")]
|
# 继续爬取模式(skip_seed): 有缓存队列时跳过起始网址, 直接从待爬队列接着爬
|
||||||
|
skip_seed = bool(self.run.get("skip_seed"))
|
||||||
|
queue = []
|
||||||
|
if not (skip_seed and pending):
|
||||||
|
queue.append((seed, 0, ""))
|
||||||
if pending:
|
if pending:
|
||||||
queue.extend((p["url"], p.get("depth", 0), p.get("source", "")) for p in pending)
|
queue.extend((p["url"], p.get("depth", 0), p.get("source", "")) for p in pending)
|
||||||
queued = set(visited)
|
queued = set(visited)
|
||||||
@@ -630,9 +636,15 @@ class CrawlJob:
|
|||||||
try:
|
try:
|
||||||
while queue and not self._stop.is_set():
|
while queue and not self._stop.is_set():
|
||||||
self._wait_if_paused()
|
self._wait_if_paused()
|
||||||
if max_pages > 0 and len(visited) >= max_pages:
|
if max_pages > 0:
|
||||||
self._log("info", f"达到最大页数上限 {max_pages}, 停止")
|
if skip_seed:
|
||||||
break
|
# 继续爬取模式: 页数上限按本次新增页数重新计算 (累计 visited 不阻塞继续)
|
||||||
|
if len(visited) - visited_base >= max_pages:
|
||||||
|
self._log("info", f"本次继续爬取达到页数上限 {max_pages}, 停止")
|
||||||
|
break
|
||||||
|
elif len(visited) >= max_pages:
|
||||||
|
self._log("info", f"达到最大页数上限 {max_pages}, 停止")
|
||||||
|
break
|
||||||
url, depth, src = queue.pop(0)
|
url, depth, src = queue.pop(0)
|
||||||
key = normalize_url(url)
|
key = normalize_url(url)
|
||||||
if key in visited and url != seed: # 起始网址不去重, 其余已爬跳过
|
if key in visited and url != seed: # 起始网址不去重, 其余已爬跳过
|
||||||
|
|||||||
+13
-1
@@ -602,7 +602,10 @@ function renderDetail() {
|
|||||||
${t.running ? `
|
${t.running ? `
|
||||||
<button class="btn" onclick="actTask('${t.id}','pause')">⏸ 暂停</button>
|
<button class="btn" onclick="actTask('${t.id}','pause')">⏸ 暂停</button>
|
||||||
<button class="btn danger" onclick="actTask('${t.id}','stop')">⏹ 终止</button>`
|
<button class="btn danger" onclick="actTask('${t.id}','stop')">⏹ 终止</button>`
|
||||||
: `<button class="btn primary" onclick="actTask('${t.id}','start')">▶ 立即执行</button>`}
|
: `
|
||||||
|
<button class="btn primary" onclick="actTask('${t.id}','start')">▶ 立即执行</button>
|
||||||
|
${t.mode === "auto" && (t.auto_pending_count || 0) > 0 ? `
|
||||||
|
<button class="btn" onclick="contTask('${t.id}')">⏩ 继续爬取(缓存 ${t.auto_pending_count} 条)</button>` : ""}`}
|
||||||
${t.mode === "auto" ? `<button class="btn" onclick="probeTask('${t.id}')">🧪 试爬取</button>
|
${t.mode === "auto" ? `<button class="btn" onclick="probeTask('${t.id}')">🧪 试爬取</button>
|
||||||
<button class="btn" ${t.running ? "disabled" : ""} onclick="clearCache('${t.id}')">🧹 清空缓存</button>` : ""}
|
<button class="btn" ${t.running ? "disabled" : ""} onclick="clearCache('${t.id}')">🧹 清空缓存</button>` : ""}
|
||||||
<button class="btn" onclick="openEdit('${t.id}')">✏️ 编辑配置</button>
|
<button class="btn" onclick="openEdit('${t.id}')">✏️ 编辑配置</button>
|
||||||
@@ -734,6 +737,15 @@ function stopLogPoll() {
|
|||||||
if (state.logTimer) { clearInterval(state.logTimer); state.logTimer = null; }
|
if (state.logTimer) { clearInterval(state.logTimer); state.logTimer = null; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function contTask(tid) {
|
||||||
|
try {
|
||||||
|
const r = await api(`/api/tasks/${tid}/continue`, { method: "POST" });
|
||||||
|
toast("已开始继续爬取缓存队列");
|
||||||
|
loadTasks();
|
||||||
|
if (state.detail.task && state.detail.task.id === tid) openDetail(tid);
|
||||||
|
} catch (e) { toast(e.message, true); }
|
||||||
|
}
|
||||||
|
|
||||||
/* ---------------- 打包导出 ---------------- */
|
/* ---------------- 打包导出 ---------------- */
|
||||||
async function exportZip(tid) {
|
async function exportZip(tid) {
|
||||||
const btn = window.event && window.event.target;
|
const btn = window.event && window.event.target;
|
||||||
|
|||||||
Reference in New Issue
Block a user