@jayyuen66/dsh-scrapling 0.1.6 → 0.1.7
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/CHANGELOG.md +34 -1
- package/README.md +6 -5
- package/README.zh-CN.md +6 -5
- package/bin/dsh-scrapling.mjs +68 -69
- package/client.js +9 -9
- package/cordis.patch.yml +7 -3
- package/docs/DETAIL.md +6 -6
- package/docs/DETAIL.zh.md +6 -6
- package/host.js +19 -19
- package/package.json +44 -43
- package/py/bridge.py +51 -14
- package/py/extraction.py +3 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jayyuen66/dsh-scrapling",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "Scrapling 网页抓取插件(dsh):静态/浏览器抓取 + CSS/XPath 结构化抽取 + 选择器自愈 + capture_xhr 与会话网络历史 + 登录态存取 + Spider 爬虫,由常驻 Python 进程驱动。",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dsh",
|
|
@@ -75,42 +75,42 @@
|
|
|
75
75
|
"zod": "4.6.7"
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
|
-
"@deepseek-ai/cordis": "4.0.6-alpha.
|
|
79
|
-
"@deepseek-ai/cordis-plugin-include": "1.0.11-alpha.
|
|
80
|
-
"@deepseek-ai/cordis-plugin-loader": "1.0.7-alpha.
|
|
81
|
-
"@deepseek-ai/dsh-api-remotes": "0.2.2-alpha.
|
|
82
|
-
"@deepseek-ai/dsh-client-locale": "0.2.2-alpha.
|
|
83
|
-
"@deepseek-ai/dsh-client-store": "0.2.2-alpha.
|
|
84
|
-
"@deepseek-ai/dsh-client-ui-chat": "0.2.2-alpha.
|
|
85
|
-
"@deepseek-ai/dsh-client-ui-conversation": "0.2.2-alpha.
|
|
86
|
-
"@deepseek-ai/dsh-client-ui-layout": "0.2.2-alpha.
|
|
87
|
-
"@deepseek-ai/dsh-client-ui-plugin-manager": "0.2.2-alpha.
|
|
88
|
-
"@deepseek-ai/dsh-client-ui-renderer": "0.2.2-alpha.
|
|
89
|
-
"@deepseek-ai/dsh-client-ui-settings": "0.2.2-alpha.
|
|
90
|
-
"@deepseek-ai/dsh-client-ui-slots": "0.2.2-alpha.
|
|
91
|
-
"@deepseek-ai/dsh-client-ui-tool": "0.2.2-alpha.
|
|
92
|
-
"@deepseek-ai/dsh-host-webserver": "0.2.2-alpha.
|
|
93
|
-
"@deepseek-ai/dsh-http-proxy": "0.2.2-alpha.
|
|
94
|
-
"@deepseek-ai/dsh-jobs": "0.2.2-alpha.
|
|
95
|
-
"@deepseek-ai/dsh-jobs-local": "0.2.2-alpha.
|
|
96
|
-
"@deepseek-ai/dsh-output-retention": "0.2.2-alpha.
|
|
97
|
-
"@deepseek-ai/dsh-package-manifest": "0.2.2-alpha.
|
|
98
|
-
"@deepseek-ai/dsh-ptc-runtime": "0.2.2-alpha.
|
|
99
|
-
"@deepseek-ai/dsh-sandbox": "0.2.2-alpha.
|
|
100
|
-
"@deepseek-ai/dsh-scope": "0.2.2-alpha.
|
|
101
|
-
"@deepseek-ai/dsh-session": "0.2.2-alpha.
|
|
102
|
-
"@deepseek-ai/dsh-settings": "0.2.2-alpha.
|
|
103
|
-
"@deepseek-ai/dsh-shell": "0.2.2-alpha.
|
|
104
|
-
"@deepseek-ai/dsh-subprocess": "0.2.2-alpha.
|
|
105
|
-
"@deepseek-ai/dsh-system-prompt": "0.2.2-alpha.
|
|
106
|
-
"@deepseek-ai/dsh-tool-executor-native": "0.2.2-alpha.
|
|
107
|
-
"@deepseek-ai/dsh-tools": "0.2.2-alpha.
|
|
108
|
-
"@deepseek-ai/dsh-typert-protocol": "0.2.2-alpha.
|
|
109
|
-
"@deepseek-ai/dsh-user-approval": "0.2.2-alpha.
|
|
110
|
-
"@deepseek-ai/dsh-web": "0.2.2-alpha.
|
|
111
|
-
"@deepseek-ai/schemastery": "3.18.6-alpha.
|
|
78
|
+
"@deepseek-ai/cordis": "4.0.6-alpha.2",
|
|
79
|
+
"@deepseek-ai/cordis-plugin-include": "1.0.11-alpha.2",
|
|
80
|
+
"@deepseek-ai/cordis-plugin-loader": "1.0.7-alpha.2",
|
|
81
|
+
"@deepseek-ai/dsh-api-remotes": "0.2.2-alpha.3",
|
|
82
|
+
"@deepseek-ai/dsh-client-locale": "0.2.2-alpha.3",
|
|
83
|
+
"@deepseek-ai/dsh-client-store": "0.2.2-alpha.3",
|
|
84
|
+
"@deepseek-ai/dsh-client-ui-chat": "0.2.2-alpha.3",
|
|
85
|
+
"@deepseek-ai/dsh-client-ui-conversation": "0.2.2-alpha.3",
|
|
86
|
+
"@deepseek-ai/dsh-client-ui-layout": "0.2.2-alpha.3",
|
|
87
|
+
"@deepseek-ai/dsh-client-ui-plugin-manager": "0.2.2-alpha.3",
|
|
88
|
+
"@deepseek-ai/dsh-client-ui-renderer": "0.2.2-alpha.3",
|
|
89
|
+
"@deepseek-ai/dsh-client-ui-settings": "0.2.2-alpha.3",
|
|
90
|
+
"@deepseek-ai/dsh-client-ui-slots": "0.2.2-alpha.3",
|
|
91
|
+
"@deepseek-ai/dsh-client-ui-tool": "0.2.2-alpha.3",
|
|
92
|
+
"@deepseek-ai/dsh-host-webserver": "0.2.2-alpha.3",
|
|
93
|
+
"@deepseek-ai/dsh-http-proxy": "0.2.2-alpha.3",
|
|
94
|
+
"@deepseek-ai/dsh-jobs": "0.2.2-alpha.3",
|
|
95
|
+
"@deepseek-ai/dsh-jobs-local": "0.2.2-alpha.3",
|
|
96
|
+
"@deepseek-ai/dsh-output-retention": "0.2.2-alpha.3",
|
|
97
|
+
"@deepseek-ai/dsh-package-manifest": "0.2.2-alpha.3",
|
|
98
|
+
"@deepseek-ai/dsh-ptc-runtime": "0.2.2-alpha.3",
|
|
99
|
+
"@deepseek-ai/dsh-sandbox": "0.2.2-alpha.3",
|
|
100
|
+
"@deepseek-ai/dsh-scope": "0.2.2-alpha.3",
|
|
101
|
+
"@deepseek-ai/dsh-session": "0.2.2-alpha.3",
|
|
102
|
+
"@deepseek-ai/dsh-settings": "0.2.2-alpha.3",
|
|
103
|
+
"@deepseek-ai/dsh-shell": "0.2.2-alpha.3",
|
|
104
|
+
"@deepseek-ai/dsh-subprocess": "0.2.2-alpha.3",
|
|
105
|
+
"@deepseek-ai/dsh-system-prompt": "0.2.2-alpha.3",
|
|
106
|
+
"@deepseek-ai/dsh-tool-executor-native": "0.2.2-alpha.3",
|
|
107
|
+
"@deepseek-ai/dsh-tools": "0.2.2-alpha.3",
|
|
108
|
+
"@deepseek-ai/dsh-typert-protocol": "0.2.2-alpha.3",
|
|
109
|
+
"@deepseek-ai/dsh-user-approval": "0.2.2-alpha.3",
|
|
110
|
+
"@deepseek-ai/dsh-web": "0.2.2-alpha.3",
|
|
111
|
+
"@deepseek-ai/schemastery": "3.18.6-alpha.2",
|
|
112
112
|
"@e18e/eslint-plugin": "0.8.1",
|
|
113
|
-
"@jayyuen66/dsh-plugin-shared": "0.2.
|
|
113
|
+
"@jayyuen66/dsh-plugin-shared": "0.2.3",
|
|
114
114
|
"@types/node": "26.6.6",
|
|
115
115
|
"@types/react": "19.3.0",
|
|
116
116
|
"@vitest/coverage-v8": "5.0.5",
|
|
@@ -128,11 +128,11 @@
|
|
|
128
128
|
"vitest": "5.0.5"
|
|
129
129
|
},
|
|
130
130
|
"peerDependencies": {
|
|
131
|
-
"@deepseek-ai/dsh": "^0.2.1-rc.3",
|
|
132
|
-
"@deepseek-ai/dsh-output-retention": "^0.2.1-rc.3",
|
|
133
|
-
"@deepseek-ai/dsh-tools": "^0.2.1-rc.3",
|
|
134
|
-
"@deepseek-ai/dsh-typert-protocol": "^0.2.1-rc.3",
|
|
135
|
-
"@deepseek-ai/dsh-web": "^0.2.1-rc.3",
|
|
131
|
+
"@deepseek-ai/dsh": "^0.2.1-rc.5 || ^0.2.2-alpha.3",
|
|
132
|
+
"@deepseek-ai/dsh-output-retention": "^0.2.1-rc.5 || ^0.2.2-alpha.3",
|
|
133
|
+
"@deepseek-ai/dsh-tools": "^0.2.1-rc.5 || ^0.2.2-alpha.3",
|
|
134
|
+
"@deepseek-ai/dsh-typert-protocol": "^0.2.1-rc.5 || ^0.2.2-alpha.3",
|
|
135
|
+
"@deepseek-ai/dsh-web": "^0.2.1-rc.5 || ^0.2.2-alpha.3",
|
|
136
136
|
"@deepseek-ai/schemastery": "^3.18.6-alpha.1"
|
|
137
137
|
},
|
|
138
138
|
"peerDependenciesMeta": {
|
|
@@ -145,7 +145,7 @@
|
|
|
145
145
|
},
|
|
146
146
|
"icon": "icon.svg",
|
|
147
147
|
"engines": {
|
|
148
|
-
"dsh": "^0.2.1-rc.3",
|
|
148
|
+
"dsh": "^0.2.1-rc.5 || ^0.2.2-alpha.3",
|
|
149
149
|
"node": ">=22.18.0"
|
|
150
150
|
},
|
|
151
151
|
"dsh": {
|
|
@@ -165,11 +165,12 @@
|
|
|
165
165
|
"lint": "oxlint .",
|
|
166
166
|
"test": "vitest run --coverage",
|
|
167
167
|
"test:watch": "vitest",
|
|
168
|
-
"check": "npm run --loglevel=warn typecheck && npm run --loglevel=warn lint && npm run --loglevel=warn build:client && npm run --loglevel=warn build && npm run --loglevel=warn test && npm run --loglevel=warn fmt:check && npm run --loglevel=warn check:py",
|
|
168
|
+
"check": "npm run --loglevel=warn typecheck && npm run --loglevel=warn lint && npm run --loglevel=warn build:client && npm run --loglevel=warn build && npm run --loglevel=warn test && npm run --loglevel=warn gate:eol && npm run --loglevel=warn fmt:check && npm run --loglevel=warn check:py",
|
|
169
169
|
"fmt:check": "oxfmt --check .",
|
|
170
170
|
"py": "node py-launcher.mjs",
|
|
171
171
|
"py:which": "node py-launcher.mjs --which",
|
|
172
172
|
"face:py": "node face-py.mjs",
|
|
173
|
+
"gate:eol": "node eol-gate.mjs",
|
|
173
174
|
"lint:py": "npm run --loglevel=warn py -- -m ruff check py/",
|
|
174
175
|
"fmt:py": "npm run --loglevel=warn py -- -m ruff format --check py/",
|
|
175
176
|
"typecheck:py": "npm run --loglevel=warn py -- -m mypy py/",
|
package/py/bridge.py
CHANGED
|
@@ -105,6 +105,10 @@ MAX_FRAME_BYTES = 8 * 1024 * 1024
|
|
|
105
105
|
# 所以正常情况下的深度约等于 MAX_INFLIGHT;超过它说明调用方在以远超并发的速率灌帧,
|
|
106
106
|
# 继续收就是纯粹的内存增长。判否让调用方自己退避。
|
|
107
107
|
MAX_QUEUE_DEPTH = MAX_INFLIGHT * 4
|
|
108
|
+
# 关机时每一路 close(爬虫、会话)的最长等待秒数。取 10 秒:浏览器会话的退出要给
|
|
109
|
+
# Chromium 进程留收尾时间,而 sidecar 自己已经不再服务任何请求,等更久只是把
|
|
110
|
+
# 「有界关机」变回无界。
|
|
111
|
+
SHUTDOWN_CLOSE_TIMEOUT = 10.0
|
|
108
112
|
|
|
109
113
|
_WRITE_LOCK = threading.Lock()
|
|
110
114
|
_SLOTS = threading.Semaphore(MAX_INFLIGHT)
|
|
@@ -300,6 +304,21 @@ def _ensure_worker(count: int) -> None:
|
|
|
300
304
|
worker.start()
|
|
301
305
|
|
|
302
306
|
|
|
307
|
+
def _run_bounded(label: str, action: Callable[[], None], timeout: float) -> None:
|
|
308
|
+
"""在后台线程里跑一次关闭并等它有界完成。
|
|
309
|
+
|
|
310
|
+
`close_all_crawls()` / `close_all_sessions()` 自己没有超时:它们要等 handler 释放
|
|
311
|
+
持有的锁,而那时 drain 已经放弃等待,关机就会从「有界」变成等一个不确定的对端。
|
|
312
|
+
超时后不重跑也不硬等——进程马上要退,这些资源由 OS 收;留一行日志是为了让宿主侧
|
|
313
|
+
读得出「这一轮是超时收的场」而不是「关干净了」。
|
|
314
|
+
"""
|
|
315
|
+
worker = threading.Thread(target=action, daemon=True, name=f"bridge-{label}")
|
|
316
|
+
worker.start()
|
|
317
|
+
worker.join(timeout)
|
|
318
|
+
if worker.is_alive():
|
|
319
|
+
log(f"shutdown: {label} still running after {timeout}s; leaving it to process exit")
|
|
320
|
+
|
|
321
|
+
|
|
303
322
|
def _shutdown_crawls() -> None:
|
|
304
323
|
"""退出前叫停全部爬虫,并给它们的线程一点收尾时间。
|
|
305
324
|
|
|
@@ -307,12 +326,16 @@ def _shutdown_crawls() -> None:
|
|
|
307
326
|
不读这一路了,帧落进管道就是没人认领的字节。所以爬虫要在会话之前关:蜘蛛自己也要用
|
|
308
327
|
浏览器,先把会话抽走只会让它在下一次请求里抛。
|
|
309
328
|
"""
|
|
310
|
-
try:
|
|
311
|
-
import crawl
|
|
312
329
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
330
|
+
def close() -> None:
|
|
331
|
+
try:
|
|
332
|
+
import crawl
|
|
333
|
+
|
|
334
|
+
crawl.close_all_crawls()
|
|
335
|
+
except Exception as error: # noqa: BLE001 —— 关不掉也不能拖住关机
|
|
336
|
+
log(f"failed to stop crawls: {type(error).__name__}: {error}")
|
|
337
|
+
|
|
338
|
+
_run_bounded("close-crawls", close, SHUTDOWN_CLOSE_TIMEOUT)
|
|
316
339
|
|
|
317
340
|
|
|
318
341
|
def _shutdown_sessions() -> None:
|
|
@@ -320,12 +343,16 @@ def _shutdown_sessions() -> None:
|
|
|
320
343
|
|
|
321
344
|
浏览器会话是独立进程:sidecar 自己退了、Chromium 还活着,就成了没人管的孤儿。
|
|
322
345
|
"""
|
|
323
|
-
try:
|
|
324
|
-
import adapters
|
|
325
346
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
347
|
+
def close() -> None:
|
|
348
|
+
try:
|
|
349
|
+
import adapters
|
|
350
|
+
|
|
351
|
+
adapters.close_all_sessions()
|
|
352
|
+
except Exception as error: # noqa: BLE001 —— 关不掉也不能拖住关机
|
|
353
|
+
log(f"failed to close sessions: {type(error).__name__}: {error}")
|
|
354
|
+
|
|
355
|
+
_run_bounded("close-sessions", close, SHUTDOWN_CLOSE_TIMEOUT)
|
|
329
356
|
|
|
330
357
|
|
|
331
358
|
def _drain_before_exit(timeout: float = 5.0) -> None:
|
|
@@ -333,20 +360,30 @@ def _drain_before_exit(timeout: float = 5.0) -> None:
|
|
|
333
360
|
|
|
334
361
|
收到 shutdown 就直接 return 的话,队列里已排队的请求会被静默丢掉 —— Host 侧看到的
|
|
335
362
|
是「没等到响应」而不是「被关机打断」。所以这里有界地等一下:等空了就走,等不到就
|
|
336
|
-
|
|
363
|
+
把还排着的帧逐条点名再走,不无限期拖住关机。
|
|
364
|
+
|
|
365
|
+
点名只覆盖队列里的帧:已经出队、正在 handler 里跑的请求没有一处集中的登记面,要命名
|
|
366
|
+
它们就得在 worker 的热路径上加簿记,这比一条关机日志不值。那一路的后果由
|
|
367
|
+
`_shutdown_crawls` / `_shutdown_sessions` 的有界关闭兜住(宿主侧等的是帧,不是关闭)。
|
|
337
368
|
|
|
338
369
|
:param timeout - 最长等待秒数
|
|
339
370
|
"""
|
|
340
371
|
deadline = time.monotonic() + timeout
|
|
341
|
-
while
|
|
372
|
+
while True:
|
|
342
373
|
with _QUEUE_LOCK:
|
|
343
|
-
pending =
|
|
374
|
+
pending = list(_QUEUE)
|
|
344
375
|
with _WORKERS_LOCK:
|
|
345
376
|
busy = any(item.is_alive() for item in _WORKERS)
|
|
346
377
|
if not pending and not busy:
|
|
347
378
|
return
|
|
379
|
+
if time.monotonic() >= deadline:
|
|
380
|
+
ids = [str(item.get("id", "(无 id)")) for item in pending]
|
|
381
|
+
log(
|
|
382
|
+
f"shutdown: giving up after {timeout}s — {len(ids)} 帧仍在队列里未应答 "
|
|
383
|
+
f"ids={','.join(ids) if ids else '(空)'}"
|
|
384
|
+
)
|
|
385
|
+
return
|
|
348
386
|
time.sleep(0.02)
|
|
349
|
-
log("shutdown: giving up on in-flight requests")
|
|
350
387
|
|
|
351
388
|
|
|
352
389
|
def read_loop() -> int:
|
package/py/extraction.py
CHANGED
|
@@ -62,8 +62,8 @@ def _resolve_url(page: Any, value: str) -> str:
|
|
|
62
62
|
return value
|
|
63
63
|
|
|
64
64
|
|
|
65
|
-
def _query_type(
|
|
66
|
-
"""把旧的 ``useXpath`` 布尔值归一到 queryType。"""
|
|
65
|
+
def _query_type(query_type: str | None, use_xpath: bool) -> str:
|
|
66
|
+
"""把旧的 ``useXpath`` 布尔值归一到 queryType:显式 queryType 优先,其次 useXpath,最后 css。"""
|
|
67
67
|
return query_type or ("xpath" if use_xpath else "css")
|
|
68
68
|
|
|
69
69
|
|
|
@@ -86,7 +86,7 @@ def query_nodes(
|
|
|
86
86
|
"""
|
|
87
87
|
if not isinstance(selector, str) or not selector:
|
|
88
88
|
raise ValueError("selector is required")
|
|
89
|
-
kind = _query_type(
|
|
89
|
+
kind = _query_type(query_type, use_xpath)
|
|
90
90
|
if kind == "css":
|
|
91
91
|
return list(page.css(selector)), ""
|
|
92
92
|
if kind == "xpath":
|