@jayyuen66/dsh-scrapling 0.1.4 → 0.1.6
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 +84 -3
- package/LICENSE +1 -1
- package/README.md +15 -15
- package/README.zh-CN.md +13 -13
- package/THIRD_PARTY_NOTICES.md +32 -10
- package/bin/dsh-scrapling.mjs +118 -11
- package/client.js +110 -35
- package/docs/DETAIL.md +41 -0
- package/docs/DETAIL.zh.md +41 -0
- package/host.js +14 -30
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +61 -47
- package/py/adapters.py +30 -8
- package/py/bridge.py +88 -18
- package/py/crawl.py +69 -27
- package/py/extraction.py +67 -21
- package/py/guard.py +55 -24
- package/py/media.py +30 -9
- package/py/redact.py +20 -2
- package/py/redis_storage.py +92 -31
- package/py/search.py +9 -2
- package/py/session_page.py +57 -24
package/py/session_page.py
CHANGED
|
@@ -107,22 +107,29 @@ def _require_selector(action: dict[str, Any], kind: str) -> str:
|
|
|
107
107
|
return target
|
|
108
108
|
|
|
109
109
|
|
|
110
|
-
def _perform(page: Any, action: dict[str, Any], timeout_ms: int) ->
|
|
111
|
-
"""
|
|
110
|
+
def _perform(page: Any, action: dict[str, Any], timeout_ms: int) -> Any:
|
|
111
|
+
"""执行一个已校验的动作;dialog 注册的处理器会返回,由调用方在批次结束时摘掉。"""
|
|
112
112
|
kind = str(action["type"])
|
|
113
113
|
if kind == "wait_time":
|
|
114
114
|
page.wait_for_timeout(int(action["ms"]))
|
|
115
|
-
return
|
|
115
|
+
return None
|
|
116
116
|
if kind == "wait_load":
|
|
117
117
|
page.wait_for_load_state(str(action.get("state", "load")), timeout=timeout_ms)
|
|
118
|
-
return
|
|
118
|
+
return None
|
|
119
119
|
if kind == "wheel":
|
|
120
120
|
page.mouse.wheel(float(action["deltaX"]), float(action["deltaY"]))
|
|
121
|
-
return
|
|
121
|
+
return None
|
|
122
122
|
if kind == "dialog":
|
|
123
123
|
accept = action.get("accept", True) is not False
|
|
124
|
-
|
|
125
|
-
|
|
124
|
+
|
|
125
|
+
def handler(dialog: Any) -> None:
|
|
126
|
+
if accept:
|
|
127
|
+
dialog.accept()
|
|
128
|
+
else:
|
|
129
|
+
dialog.dismiss()
|
|
130
|
+
|
|
131
|
+
page.once("dialog", handler)
|
|
132
|
+
return handler
|
|
126
133
|
if kind == "press_key":
|
|
127
134
|
key = str(action.get("key", ""))
|
|
128
135
|
target = action.get("target")
|
|
@@ -130,10 +137,10 @@ def _perform(page: Any, action: dict[str, Any], timeout_ms: int) -> None:
|
|
|
130
137
|
page.locator(target).press(key, timeout=timeout_ms)
|
|
131
138
|
else:
|
|
132
139
|
page.keyboard.press(key)
|
|
133
|
-
return
|
|
140
|
+
return None
|
|
134
141
|
if kind == "move" and not isinstance(action.get("target"), str):
|
|
135
142
|
page.mouse.move(float(action["x"]), float(action["y"]))
|
|
136
|
-
return
|
|
143
|
+
return None
|
|
137
144
|
|
|
138
145
|
selector = _require_selector(action, kind)
|
|
139
146
|
locator = page.locator(selector)
|
|
@@ -156,6 +163,7 @@ def _perform(page: Any, action: dict[str, Any], timeout_ms: int) -> None:
|
|
|
156
163
|
locator.wait_for(state=str(action.get("state", "visible")), timeout=timeout_ms)
|
|
157
164
|
else: # pragma: no cover —— 类型白名单已挡住
|
|
158
165
|
raise _adapter_error("ACTION_INVALID", f"unknown action type: {kind}")
|
|
166
|
+
return None
|
|
159
167
|
|
|
160
168
|
|
|
161
169
|
def _validate_actions(value: Any, max_actions: int) -> list[dict[str, Any]]:
|
|
@@ -168,17 +176,21 @@ def _validate_actions(value: Any, max_actions: int) -> list[dict[str, Any]]:
|
|
|
168
176
|
if not isinstance(item, dict):
|
|
169
177
|
raise _adapter_error("ACTION_INVALID", f"action {index} must be an object")
|
|
170
178
|
kind = item.get("type")
|
|
171
|
-
|
|
179
|
+
# frozenset 的成员判断先哈希:type 写成 list/dict 会抛 TypeError 而不是 ACTION_INVALID,
|
|
180
|
+
# 宿主看到的就是内部异常。非字符串与未知类型在这里走同一条判据。
|
|
181
|
+
if not isinstance(kind, str) or kind not in _ACTION_TYPES:
|
|
172
182
|
raise _adapter_error("ACTION_INVALID", f"action {index} has an unknown type: {kind!r}")
|
|
173
183
|
action = dict(item)
|
|
174
184
|
if kind in _SELECTOR_ACTIONS:
|
|
175
185
|
_require_selector(action, str(kind))
|
|
176
|
-
if
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
186
|
+
if kind == "move":
|
|
187
|
+
target = action.get("target")
|
|
188
|
+
# 空串 target 不算 target:_perform 会把它当「有 target」交给 locator,
|
|
189
|
+
# 执行期再抛 ACTION_INVALID 又被包装成 ACTION_FAILED,错误码与下标语义都错位。
|
|
190
|
+
has_target = isinstance(target, str) and bool(target)
|
|
191
|
+
has_point = isinstance(action.get("x"), (int, float)) and isinstance(action.get("y"), (int, float))
|
|
192
|
+
if not has_target and not has_point:
|
|
193
|
+
raise _adapter_error("ACTION_INVALID", "move needs a non-empty target selector or x/y coordinates")
|
|
182
194
|
if kind == "wheel" and (
|
|
183
195
|
not isinstance(action.get("deltaX"), (int, float)) or not isinstance(action.get("deltaY"), (int, float))
|
|
184
196
|
):
|
|
@@ -189,20 +201,32 @@ def _validate_actions(value: Any, max_actions: int) -> list[dict[str, Any]]:
|
|
|
189
201
|
raise _adapter_error("ACTION_INVALID", "wait_time needs a non-negative ms")
|
|
190
202
|
if kind == "wait_element":
|
|
191
203
|
state = action.get("state", "visible")
|
|
192
|
-
if state not in _ELEMENT_STATES:
|
|
204
|
+
if not isinstance(state, str) or state not in _ELEMENT_STATES:
|
|
193
205
|
raise _adapter_error("ACTION_INVALID", f"unknown element state: {state!r}")
|
|
194
206
|
if kind == "wait_load":
|
|
195
207
|
state = action.get("state", "load")
|
|
196
|
-
if state not in _LOAD_STATES:
|
|
208
|
+
if not isinstance(state, str) or state not in _LOAD_STATES:
|
|
197
209
|
raise _adapter_error("ACTION_INVALID", f"unknown load state: {state!r}")
|
|
198
|
-
if kind == "press_key"
|
|
199
|
-
|
|
210
|
+
if kind == "press_key":
|
|
211
|
+
key = action.get("key")
|
|
212
|
+
# 空字符串 key 能过 isinstance,执行期才由 Playwright 抛错并被包装成 ACTION_FAILED。
|
|
213
|
+
if not isinstance(key, str) or not key:
|
|
214
|
+
raise _adapter_error("ACTION_INVALID", "press_key needs a non-empty key")
|
|
215
|
+
if kind == "checkbox" and not isinstance(action.get("checked", True), bool):
|
|
216
|
+
raise _adapter_error("ACTION_INVALID", "checkbox checked must be a boolean")
|
|
200
217
|
if kind == "dialog" and not isinstance(action.get("accept", True), bool):
|
|
201
218
|
raise _adapter_error("ACTION_INVALID", "dialog accept must be a boolean")
|
|
202
219
|
actions.append(action)
|
|
203
220
|
return actions
|
|
204
221
|
|
|
205
222
|
|
|
223
|
+
def _release_dialog_handler(page: Any, handler: Any) -> None:
|
|
224
|
+
"""摘掉一个 dialog 处理器;页已关闭时不必再碰它(处理器随页一起消失)。"""
|
|
225
|
+
if page.is_closed():
|
|
226
|
+
return
|
|
227
|
+
page.remove_listener("dialog", handler)
|
|
228
|
+
|
|
229
|
+
|
|
206
230
|
def run_actions(
|
|
207
231
|
session: Any,
|
|
208
232
|
*,
|
|
@@ -216,25 +240,34 @@ def run_actions(
|
|
|
216
240
|
page_info = _ready_page(session)
|
|
217
241
|
page = page_info.page
|
|
218
242
|
completed = 0
|
|
243
|
+
dialog_handlers: list[Any] = []
|
|
219
244
|
try:
|
|
220
245
|
if page.is_closed():
|
|
221
246
|
raise _adapter_error("PAGE_BUSY", "the session's page has been closed")
|
|
222
247
|
for index, action in enumerate(prepared, start=1):
|
|
223
248
|
try:
|
|
224
|
-
_perform(page, action, timeout_ms)
|
|
249
|
+
handler = _perform(page, action, timeout_ms)
|
|
250
|
+
if handler is not None:
|
|
251
|
+
dialog_handlers.append(handler)
|
|
252
|
+
completed = index
|
|
253
|
+
if slowly:
|
|
254
|
+
page.wait_for_timeout(100)
|
|
225
255
|
except Exception as error:
|
|
256
|
+
# 慢速间隔也在这一层里:页在动作之间被关掉时同样按 ACTION_FAILED 点名下标,
|
|
257
|
+
# 而不是把裸异常漏给宿主。
|
|
226
258
|
raise _adapter_error(
|
|
227
259
|
"ACTION_FAILED",
|
|
228
260
|
f"action {index} ({action['type']}) failed: {type(error).__name__}: {error}",
|
|
229
261
|
details={"actionIndex": index},
|
|
230
262
|
) from error
|
|
231
|
-
completed = index
|
|
232
|
-
if slowly:
|
|
233
|
-
page.wait_for_timeout(100)
|
|
234
263
|
return {
|
|
235
264
|
"completed": completed,
|
|
236
265
|
"finalUrl": str(page.url),
|
|
237
266
|
"title": str(page.title()),
|
|
238
267
|
}
|
|
239
268
|
finally:
|
|
269
|
+
# 页面在共享的 page pool 里跨请求复用:批次一结束就摘掉 dialog 处理器,
|
|
270
|
+
# 否则后续请求弹出的对话框会被这一次动作留下的处理器替用户应答。
|
|
271
|
+
for handler in dialog_handlers:
|
|
272
|
+
_release_dialog_handler(page, handler)
|
|
240
273
|
_finish_page(session.page_pool, page_info)
|