@jayyuen66/dsh-scrapling 0.1.15 → 0.1.17
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 +21 -0
- package/README.md +50 -26
- package/README.zh-CN.md +51 -27
- package/THIRD_PARTY_NOTICES.md +4 -2
- package/bin/dsh-scrapling.mjs +71 -71
- package/client.js +2 -8
- package/cordis.patch.yml +1 -1
- package/docs/DETAIL.md +16 -7
- package/docs/DETAIL.zh.md +16 -7
- package/host.js +25 -21
- package/package.json +49 -46
- package/py/adapters.py +190 -44
- package/py/bridge.py +77 -5
- package/py/cancellation.py +56 -0
- package/py/document.py +128 -0
- package/py/documents-requirements.txt +1 -0
- package/py/media.py +3 -2
- package/py/runtime-requirements.txt +1 -0
- package/py/search.py +106 -22
- package/py/session_page.py +90 -22
- package/runtime-manifest.json +6 -0
package/py/session_page.py
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
"""
|
|
1
|
+
"""会话当前页的读取。只碰浏览器会话的 page pool,不新建浏览器。"""
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
from typing import Any
|
|
6
6
|
|
|
7
|
+
from cancellation import RequestCancelled, raise_if_cancelled
|
|
8
|
+
|
|
7
9
|
|
|
8
10
|
def _adapter_error(code: str, message: str, *, details: dict[str, Any] | None = None) -> Exception:
|
|
9
11
|
from adapters import AdapterError
|
|
@@ -28,9 +30,12 @@ _ACTION_TYPES = frozenset(
|
|
|
28
30
|
"wait_time",
|
|
29
31
|
"wait_element",
|
|
30
32
|
"wait_load",
|
|
33
|
+
"drag",
|
|
31
34
|
}
|
|
32
35
|
)
|
|
33
36
|
_SELECTOR_ACTIONS = frozenset({"click", "textbox", "checkbox", "radio", "combobox", "wait_element"})
|
|
37
|
+
_SNAPSHOT_TYPES = frozenset({"text", "aria"})
|
|
38
|
+
_DRAG_COORDINATES = ("fromX", "fromY", "toX", "toY")
|
|
34
39
|
_ELEMENT_STATES = frozenset({"attached", "detached", "hidden", "visible"})
|
|
35
40
|
_LOAD_STATES = frozenset({"load", "domcontentloaded", "networkidle"})
|
|
36
41
|
|
|
@@ -107,41 +112,67 @@ def read_current_page(
|
|
|
107
112
|
max_content_chars: int,
|
|
108
113
|
include_metadata: bool,
|
|
109
114
|
strip_inline_images: bool,
|
|
115
|
+
snapshot_type: str = "text",
|
|
110
116
|
) -> dict[str, Any]:
|
|
111
|
-
"""
|
|
112
|
-
|
|
113
|
-
页面池把「没有页」与「页都被占用」分成两件事:前者是还没抓过,后者是别的请求
|
|
114
|
-
正在用这一页。两者对模型要采取的行动不同,所以不能合并成一个空正文。
|
|
115
|
-
"""
|
|
116
|
-
from adapters import _fake_response, to_payload
|
|
117
|
+
"""读当前就绪页的正文或 ARIA 快照;没有页或页不可用时给出明确错误码。"""
|
|
118
|
+
from adapters import _fake_response, clip, to_payload
|
|
117
119
|
|
|
120
|
+
if snapshot_type not in _SNAPSHOT_TYPES:
|
|
121
|
+
raise _adapter_error("BAD_ARGS", "snapshotType must be text or aria")
|
|
122
|
+
raise_if_cancelled()
|
|
118
123
|
page_info = _ready_page(session)
|
|
119
124
|
page = page_info.page
|
|
125
|
+
payload: dict[str, Any] | None = None
|
|
120
126
|
try:
|
|
121
127
|
if page.is_closed():
|
|
122
128
|
raise _closed_page_error()
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
129
|
+
if snapshot_type == "aria":
|
|
130
|
+
try:
|
|
131
|
+
raw = (page.locator(css_selector) if css_selector else page).aria_snapshot(mode="ai")
|
|
132
|
+
except Exception as error:
|
|
133
|
+
if page.is_closed():
|
|
134
|
+
raise _closed_page_error() from error
|
|
135
|
+
raise _adapter_error(
|
|
136
|
+
"SESSION_READ_FAILED",
|
|
137
|
+
f"ARIA snapshot failed: {type(error).__name__}: {error}",
|
|
138
|
+
) from error
|
|
139
|
+
raise_if_cancelled()
|
|
140
|
+
content, truncated = clip(str(raw), max_content_chars)
|
|
141
|
+
url, title = _page_identity(page)
|
|
142
|
+
else:
|
|
143
|
+
try:
|
|
144
|
+
html = page.content()
|
|
145
|
+
except Exception as error: # 读到一半页被关掉/崩溃,归成「这一页没了」
|
|
146
|
+
raise _closed_page_error() from error
|
|
147
|
+
raise_if_cancelled()
|
|
148
|
+
url, title = _page_identity(page)
|
|
149
|
+
response = _fake_response(str(html), url, 200)
|
|
150
|
+
payload = to_payload(
|
|
151
|
+
response,
|
|
152
|
+
extraction_type=extraction_type,
|
|
153
|
+
css_selector=css_selector,
|
|
154
|
+
main_content_only=main_content_only,
|
|
155
|
+
max_content_chars=max_content_chars,
|
|
156
|
+
strip_inline_images=strip_inline_images,
|
|
157
|
+
include_metadata=include_metadata,
|
|
158
|
+
)
|
|
138
159
|
finally:
|
|
139
160
|
_finish_page(session.page_pool, page_info)
|
|
161
|
+
if payload is None:
|
|
162
|
+
return {
|
|
163
|
+
"url": url,
|
|
164
|
+
"title": str(title),
|
|
165
|
+
"content": content,
|
|
166
|
+
"truncated": truncated,
|
|
167
|
+
"snapshotType": "aria",
|
|
168
|
+
"notice": "the page accessibility tree was read from the session's current DOM, not fetched again",
|
|
169
|
+
}
|
|
140
170
|
return {
|
|
141
171
|
"url": payload["url"],
|
|
142
172
|
"title": str(title),
|
|
143
173
|
"content": payload["content"],
|
|
144
174
|
"truncated": payload["truncated"],
|
|
175
|
+
"snapshotType": "text",
|
|
145
176
|
"notice": "the page was read from the session's current DOM, not fetched again",
|
|
146
177
|
**({"metadata": payload["metadata"]} if "metadata" in payload else {}),
|
|
147
178
|
}
|
|
@@ -166,12 +197,32 @@ def _has_target(action: dict[str, Any]) -> bool:
|
|
|
166
197
|
return isinstance(target, str) and bool(target)
|
|
167
198
|
|
|
168
199
|
|
|
200
|
+
def _drag_selectors(action: dict[str, Any]) -> bool:
|
|
201
|
+
return "from" in action or "to" in action
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _perform_drag(page: Any, action: dict[str, Any], timeout_ms: int) -> None:
|
|
205
|
+
if _drag_selectors(action):
|
|
206
|
+
page.locator(action["from"]).drag_to(page.locator(action["to"]), timeout=timeout_ms)
|
|
207
|
+
return
|
|
208
|
+
mouse = page.mouse
|
|
209
|
+
mouse.move(float(action["fromX"]), float(action["fromY"]))
|
|
210
|
+
mouse.down()
|
|
211
|
+
try:
|
|
212
|
+
mouse.move(float(action["toX"]), float(action["toY"]), steps=10)
|
|
213
|
+
finally:
|
|
214
|
+
mouse.up()
|
|
215
|
+
|
|
216
|
+
|
|
169
217
|
def _perform(page: Any, action: dict[str, Any], timeout_ms: int) -> Any:
|
|
170
218
|
"""执行一个已校验的动作;dialog 注册的处理器会返回,由调用方在批次结束时摘掉。"""
|
|
171
219
|
kind = str(action["type"])
|
|
172
220
|
if kind == "wait_time":
|
|
173
221
|
page.wait_for_timeout(int(action["ms"]))
|
|
174
222
|
return None
|
|
223
|
+
if kind == "drag":
|
|
224
|
+
_perform_drag(page, action, timeout_ms)
|
|
225
|
+
return None
|
|
175
226
|
if kind == "wait_load":
|
|
176
227
|
page.wait_for_load_state(str(action.get("state", "load")), timeout=timeout_ms)
|
|
177
228
|
return None
|
|
@@ -248,6 +299,16 @@ def _validate_actions(value: Any, max_actions: int) -> list[dict[str, Any]]:
|
|
|
248
299
|
raise _adapter_error("ACTION_INVALID", "move needs a non-empty target selector or x/y coordinates")
|
|
249
300
|
if kind == "wheel" and (not _number(action.get("deltaX")) or not _number(action.get("deltaY"))):
|
|
250
301
|
raise _adapter_error("ACTION_INVALID", "wheel needs numeric deltaX and deltaY")
|
|
302
|
+
if kind == "drag":
|
|
303
|
+
has_selectors = _drag_selectors(action)
|
|
304
|
+
has_coordinates = any(key in action for key in _DRAG_COORDINATES)
|
|
305
|
+
if has_selectors and has_coordinates:
|
|
306
|
+
raise _adapter_error("ACTION_INVALID", "drag cannot mix selectors and coordinates")
|
|
307
|
+
if has_selectors:
|
|
308
|
+
if not all(isinstance(action.get(key), str) and action[key] for key in ("from", "to")):
|
|
309
|
+
raise _adapter_error("ACTION_INVALID", "drag needs non-empty from/to selectors")
|
|
310
|
+
elif not all(_number(action.get(key)) for key in _DRAG_COORDINATES):
|
|
311
|
+
raise _adapter_error("ACTION_INVALID", "drag needs fromX/fromY/toX/toY coordinates")
|
|
251
312
|
if kind == "wait_time":
|
|
252
313
|
ms = action.get("ms")
|
|
253
314
|
if not isinstance(ms, int) or isinstance(ms, bool) or ms < 0:
|
|
@@ -311,6 +372,7 @@ def run_actions(
|
|
|
311
372
|
) -> dict[str, Any]:
|
|
312
373
|
"""按顺序执行结构化动作;首个失败停止并点名下标。"""
|
|
313
374
|
prepared = _validate_actions(actions, max_actions)
|
|
375
|
+
raise_if_cancelled()
|
|
314
376
|
page_info = _ready_page(session)
|
|
315
377
|
page = page_info.page
|
|
316
378
|
completed = 0
|
|
@@ -320,12 +382,17 @@ def run_actions(
|
|
|
320
382
|
raise _closed_page_error()
|
|
321
383
|
for index, action in enumerate(prepared, start=1):
|
|
322
384
|
try:
|
|
385
|
+
raise_if_cancelled()
|
|
323
386
|
handler = _perform(page, action, timeout_ms)
|
|
324
387
|
if handler is not None:
|
|
325
388
|
dialog_handlers.append(handler)
|
|
389
|
+
raise_if_cancelled()
|
|
326
390
|
completed = index
|
|
327
391
|
if slowly:
|
|
328
392
|
page.wait_for_timeout(100)
|
|
393
|
+
raise_if_cancelled()
|
|
394
|
+
except RequestCancelled:
|
|
395
|
+
raise
|
|
329
396
|
except Exception as error:
|
|
330
397
|
# 慢速间隔也在这一层里:页在动作之间被关掉时同样按 ACTION_FAILED 点名下标,
|
|
331
398
|
# 而不是把裸异常漏给宿主。
|
|
@@ -334,6 +401,7 @@ def run_actions(
|
|
|
334
401
|
f"action {index} ({action['type']}) failed: {type(error).__name__}: {error}",
|
|
335
402
|
details={"actionIndex": index},
|
|
336
403
|
) from error
|
|
404
|
+
raise_if_cancelled()
|
|
337
405
|
# 终页信息在 finally 之外读:动作全做成了才走这里,而这两项各自兜底(一次关窗或页崩溃
|
|
338
406
|
# 不该把已经完成的批次报成内部异常)。
|
|
339
407
|
url, title = _page_identity(page)
|
package/runtime-manifest.json
CHANGED
|
@@ -8,6 +8,12 @@
|
|
|
8
8
|
"sha256": "226d696642a8a53d03b4d5b255b1c319b1575a6d44bd723a953c9f507755e36a",
|
|
9
9
|
"sizeBytes": 26834117
|
|
10
10
|
},
|
|
11
|
+
"documents": {
|
|
12
|
+
"requirements": "py/documents-requirements.txt",
|
|
13
|
+
"distribution": "pypdf",
|
|
14
|
+
"version": "7.1.1",
|
|
15
|
+
"sha256": "a2a9f3b73270bbe9816db704de961a9b517ef9e98e45b2b5c2d847c623a30407"
|
|
16
|
+
},
|
|
11
17
|
"unsupported": {
|
|
12
18
|
"darwin-x64": "not verified on this host",
|
|
13
19
|
"linux-x64": "not verified on this host",
|