@capyclaw/scrapling 0.0.0-stage → 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 CapyClaw
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,264 @@
1
- # Temporary Holding Version
1
+ # dsh-scrapling
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Scrapling 网页抓取插件 - 为 DeepSeek Harness 提供强大的网页抓取能力
4
+
5
+ ## 功能特性
6
+
7
+ ### 🚀 核心功能
8
+ - **快速 HTTP 请求** - 高性能的静态页面抓取
9
+ - **隐身浏览器** - 真实浏览器渲染,绕过反爬虫检测
10
+ - **Cloudflare 绕过** - 自动处理 Cloudflare Turnstile/Interstitial
11
+ - **JavaScript 渲染** - 完整的 JS 页面支持
12
+ - **CSS/XPath 选择器** - 强大的元素选择和提取
13
+ - **并发爬虫** - 高效的多线程爬取
14
+ - **代理轮换** - 自动代理管理和轮换
15
+ - **自适应解析** - 智能元素追踪,适应页面变化
16
+ - **多格式导出** - JSON、CSV、XML 等格式
17
+
18
+ ## 安装
19
+
20
+ ### 前提条件
21
+
22
+ 需要安装 Python 和 Scrapling 包:
23
+
24
+ ```bash
25
+ # 安装 Scrapling 及所有依赖
26
+ pip install "scrapling[all]>=0.5"
27
+
28
+ # 安装浏览器引擎(用于 JavaScript 渲染)
29
+ scrapling install --force
30
+ ```
31
+
32
+ ### 插件安装
33
+
34
+ 本插件已集成在 DeepSeek Harness 的官方扩展中,通过 DSH 的插件系统自动加载。
35
+
36
+ ## 使用方法
37
+
38
+ ### API 调用
39
+
40
+ 插件通过 Cordis 依赖注入系统提供 `scrapling` 服务:
41
+
42
+ ```javascript
43
+ // 在其他插件中使用
44
+ export const inject = ['scrapling']
45
+
46
+ export async function apply(ctx) {
47
+ // HTTP 抓取
48
+ const result = await ctx.scrapling.fetch('https://example.com')
49
+ console.log(result.html)
50
+
51
+ // 浏览器模式抓取
52
+ const browserResult = await ctx.scrapling.fetchWithBrowser('https://example.com', {
53
+ waitFor: '.content',
54
+ timeout: 10000
55
+ })
56
+
57
+ // 解析 HTML
58
+ const doc = ctx.scrapling.parse(result.html)
59
+ const titles = doc.css('h1::text')
60
+ }
61
+ ```
62
+
63
+ ### Python 集成
64
+
65
+ 插件内部通过以下方式调用 Scrapling:
66
+
67
+ #### 基础抓取
68
+ ```python
69
+ from scrapling.fetchers import Fetcher
70
+
71
+ page = Fetcher.get('https://quotes.toscrape.com/')
72
+ quotes = page.css('.quote .text::text').getall()
73
+ ```
74
+
75
+ #### 浏览器模式
76
+ ```python
77
+ from scrapling.fetchers import StealthyFetcher
78
+
79
+ page = StealthyFetcher.get('https://example.com',
80
+ wait_selector='.content',
81
+ wait_timeout=10)
82
+ title = page.css('h1::text').get()
83
+ ```
84
+
85
+ #### Spider 爬虫
86
+ ```python
87
+ from scrapling.spiders import Spider, Response
88
+
89
+ class QuotesSpider(Spider):
90
+ name = "quotes"
91
+ start_urls = ["https://quotes.toscrape.com/"]
92
+
93
+ async def parse(self, response: Response):
94
+ for quote in response.css('.quote'):
95
+ yield {
96
+ "text": quote.css('.text::text').get(),
97
+ "author": quote.css('.author::text').get(),
98
+ }
99
+ ```
100
+
101
+ ## 配置选项
102
+
103
+ 在 `cordis.patch.yml` 中可以配置:
104
+
105
+ ```yaml
106
+ - insert:
107
+ - id: scrapling-host
108
+ name: dsh-scrapling
109
+ config:
110
+ enabled: true
111
+ defaultTimeout: 30000
112
+ maxConcurrentRequests: 10
113
+ ```
114
+
115
+ ## 架构设计
116
+
117
+ ### 宿主半(lib/host.mjs)
118
+ - 通过 Cordis 插件系统注册服务
119
+ - 提供 Scrapling API 的 JavaScript 封装
120
+ - 管理 Python 进程或 HTTP API 调用
121
+
122
+ ### 浏览器半(client/client.js)
123
+ - 提供前端 UI 集成
124
+ - 注册命令和快捷键
125
+ - 显示抓取状态和结果
126
+
127
+ ## API 参考
128
+
129
+ ### ctx.scrapling.fetch(url, options)
130
+
131
+ 使用 HTTP 模式抓取页面。
132
+
133
+ **参数:**
134
+ - `url` (string) - 目标 URL
135
+ - `options` (object) - 可选配置
136
+ - `headers` - 自定义请求头
137
+ - `proxy` - 代理地址
138
+ - `timeout` - 超时时间(毫秒)
139
+
140
+ **返回:** Promise<{ url, html, status }>
141
+
142
+ ### ctx.scrapling.fetchWithBrowser(url, options)
143
+
144
+ 使用浏览器模式抓取页面。
145
+
146
+ **参数:**
147
+ - `url` (string) - 目标 URL
148
+ - `options` (object) - 可选配置
149
+ - `waitFor` - 等待的选择器
150
+ - `timeout` - 超时时间(毫秒)
151
+ - `screenshot` - 是否截图
152
+
153
+ **返回:** Promise<{ url, html, screenshot? }>
154
+
155
+ ### ctx.scrapling.parse(html)
156
+
157
+ 解析 HTML 内容。
158
+
159
+ **参数:**
160
+ - `html` (string) - HTML 字符串
161
+
162
+ **返回:** ParsedDocument - 包含 css() 和 xpath() 方法的文档对象
163
+
164
+ ## 开发
165
+
166
+ ### 目录结构
167
+
168
+ ```
169
+ dsh-scrapling/
170
+ ├── lib/
171
+ │ ├── host.mjs # Cordis 插件入口
172
+ │ └── index.mjs # API 导出
173
+ ├── client/
174
+ │ └── client.js # 浏览器端代码
175
+ ├── test/
176
+ │ └── plugin.test.mjs # 测试文件
177
+ ├── cordis.patch.yml # Bundle 配置
178
+ ├── package.json # 包定义
179
+ └── README.md # 本文件
180
+ ```
181
+
182
+ ### 运行测试
183
+
184
+ ```bash
185
+ npm test
186
+ ```
187
+
188
+ ## 示例
189
+
190
+ ### 示例 1: 抓取引言网站
191
+
192
+ ```javascript
193
+ const quotes = await ctx.scrapling.fetch('https://quotes.toscrape.com/')
194
+ const doc = ctx.scrapling.parse(quotes.html)
195
+ const allQuotes = doc.css('.quote .text::text')
196
+ console.log('找到', allQuotes.length, '条引言')
197
+ ```
198
+
199
+ ### 示例 2: 处理动态页面
200
+
201
+ ```javascript
202
+ const page = await ctx.scrapling.fetchWithBrowser('https://example.com', {
203
+ waitFor: '.dynamic-content',
204
+ timeout: 10000
205
+ })
206
+ const doc = ctx.scrapling.parse(page.html)
207
+ const content = doc.css('.dynamic-content::text')
208
+ ```
209
+
210
+ ### 示例 3: 使用代理
211
+
212
+ ```javascript
213
+ const result = await ctx.scrapling.fetch('https://example.com', {
214
+ proxy: 'http://proxy.example.com:8080'
215
+ })
216
+ ```
217
+
218
+ ## 故障排除
219
+
220
+ ### Scrapling 未安装
221
+
222
+ **错误**: `ModuleNotFoundError: No module named 'scrapling'`
223
+
224
+ **解决方案**:
225
+ ```bash
226
+ pip install "scrapling[all]>=0.5"
227
+ ```
228
+
229
+ ### 浏览器未安装
230
+
231
+ **错误**: Browser 相关错误
232
+
233
+ **解决方案**:
234
+ ```bash
235
+ scrapling install --force
236
+ ```
237
+
238
+ ### 权限问题
239
+
240
+ 确保 DSH 进程有权限执行 Python 和访问网络。
241
+
242
+ ## 相关链接
243
+
244
+ - **Scrapling 官方文档**: https://scrapling.readthedocs.io
245
+ - **Scrapling GitHub**: https://github.com/D4Vinci/Scrapling
246
+ - **MCP Server 文档**: https://scrapling.readthedocs.io/en/latest/ai/mcp-server.html
247
+ - **DSH 插件开发**: ../../docs/guides/plugin-development.md
248
+
249
+ ## 许可证
250
+
251
+ MIT License
252
+
253
+ ## 作者
254
+
255
+ - **Scrapling**: D4Vinci
256
+ - **DSH 集成**: DeepSeek Harness Team
257
+
258
+ ## 版本历史
259
+
260
+ ### 1.0.0 (2026-11-04)
261
+ - 初始版本
262
+ - 基础 HTTP 抓取功能
263
+ - 浏览器模式支持
264
+ - HTML 解析功能
@@ -0,0 +1,27 @@
1
+ /**
2
+ * dsh-scrapling 浏览器半
3
+ *
4
+ * 提供 Scrapling 功能的前端界面集成
5
+ */
6
+ window.__ModuleLoader__.load({ id: '@capyclaw/scrapling', factory: (require) => {
7
+ var module = { exports: {} }; var exports = module.exports;
8
+
9
+ module.exports = {
10
+ name: 'dsh-scrapling',
11
+ inject: ['slots'],
12
+ apply: function (ctx) {
13
+ console.log('[dsh-scrapling] 客户端已加载')
14
+
15
+ // 在这里可以注册 UI 组件、命令等
16
+
17
+ // 清理函数
18
+ ctx.effect(function () {
19
+ return function () {
20
+ console.log('[dsh-scrapling] 客户端已卸载')
21
+ }
22
+ })
23
+ },
24
+ }
25
+
26
+ return module.exports;
27
+ } });
@@ -0,0 +1,9 @@
1
+ # dsh-scrapling bundle patch
2
+ # Scrapling 网页抓取插件配置
3
+
4
+ - insert:
5
+ - id: scrapling-host
6
+ name: "@capyclaw/scrapling"
7
+ config:
8
+ # Scrapling 配置选项
9
+ enabled: true
package/lib/host.mjs ADDED
@@ -0,0 +1,83 @@
1
+ /**
2
+ * dsh-scrapling 宿主半(Cordis 入口)
3
+ *
4
+ * 能力边界(诚实声明):
5
+ * - fetch() 已实现:用 Node 内置 fetch 发起 HTTP 请求,返回状态/头/正文。
6
+ * - fetchWithBrowser() 未实现:浏览器渲染 / Cloudflare 绕过需要外部 Python scrapling,
7
+ * 当前直接抛 NOT_IMPLEMENTED,不会伪造结果。
8
+ * - parse() 未实现:CSS/XPath 选择器需要 DOM 解析器,同上。
9
+ *
10
+ * 配置(resolveConfig:ctx config → 环境变量 → 默认值):
11
+ * enabled 是否注册服务 DSH_SCRAPLING_ENABLED 默认 true
12
+ * timeoutMs 单次请求超时(ms) DSH_SCRAPLING_TIMEOUT_MS 默认 20000
13
+ * userAgent 默认 UA DSH_SCRAPLING_USER_AGENT 默认 undici
14
+ */
15
+ import { resolveConfig } from 'dsh-plugin-kit/config'
16
+
17
+ export const name = '@capyclaw/scrapling'
18
+
19
+ function notImplemented(feature) {
20
+ const error = new Error(
21
+ `[dsh-scrapling] ${feature} 未实现:浏览器渲染与选择器解析依赖外部 Python scrapling 运行时。` +
22
+ '请使用 fetch() 做普通 HTTP 抓取,或另行部署 scrapling 服务后通过 HTTP 调用。',
23
+ )
24
+ error.code = 'NOT_IMPLEMENTED'
25
+ return error
26
+ }
27
+
28
+ export async function apply(ctx, config = {}) {
29
+ const { value: cfg, issues } = resolveConfig(config, {
30
+ envPrefix: 'DSH_SCRAPLING',
31
+ defaults: { enabled: true, timeoutMs: 20000, userAgent: 'undici' },
32
+ fields: {
33
+ enabled: { env: 'ENABLED', type: 'boolean' },
34
+ timeoutMs: { env: 'TIMEOUT_MS', type: 'number' },
35
+ userAgent: { env: 'USER_AGENT' },
36
+ },
37
+ })
38
+ if (issues.length) {
39
+ for (const issue of issues) ctx.logger?.warn?.(`[dsh-scrapling] 配置: ${issue}`)
40
+ }
41
+
42
+ if (!cfg.enabled) {
43
+ ctx.logger?.info?.('[dsh-scrapling] 已按配置禁用,不注册 scrapling 服务')
44
+ return
45
+ }
46
+
47
+ ctx.provide('scrapling', {
48
+ /** 普通 HTTP 抓取(真实实现)。 */
49
+ async fetch(url, options = {}) {
50
+ const response = await fetch(url, {
51
+ method: options.method || 'GET',
52
+ headers: {
53
+ 'user-agent': options.userAgent || cfg.userAgent,
54
+ ...(options.headers || {}),
55
+ },
56
+ body: options.body,
57
+ signal: AbortSignal.timeout(Number(options.timeoutMs) || cfg.timeoutMs),
58
+ })
59
+ const html = await response.text()
60
+ return {
61
+ url,
62
+ status: response.status,
63
+ ok: response.ok,
64
+ headers: Object.fromEntries(response.headers.entries()),
65
+ html,
66
+ }
67
+ },
68
+
69
+ /** 浏览器渲染:未实现。 */
70
+ async fetchWithBrowser() {
71
+ throw notImplemented('fetchWithBrowser')
72
+ },
73
+
74
+ /** 选择器解析:未实现。 */
75
+ parse() {
76
+ throw notImplemented('parse')
77
+ },
78
+ })
79
+
80
+ ctx.logger?.info?.(
81
+ `[dsh-scrapling] 已注册 scrapling 服务(仅 HTTP fetch;浏览器/解析未实现,timeout=${cfg.timeoutMs}ms)`,
82
+ )
83
+ }
package/lib/index.mjs ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * dsh-scrapling API 导出
3
+ */
4
+
5
+ export { name, apply } from './host.mjs'
package/package.json CHANGED
@@ -1,6 +1,63 @@
1
- {
2
- "name": "@capyclaw/scrapling",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "@capyclaw/scrapling",
3
+ "version": "1.0.0",
4
+ "description": "Scrapling 网页抓取集成:支持 HTTP 请求、隐身浏览器、JavaScript 渲染、Cloudflare 绕过、并发爬虫和数据导出",
5
+ "type": "module",
6
+ "main": "./lib/host.mjs",
7
+ "exports": {
8
+ ".": "./lib/host.mjs",
9
+ "./api": "./lib/index.mjs",
10
+ "./client": "./client/client.js",
11
+ "./package.json": "./package.json"
12
+ },
13
+ "dependencies": {
14
+ "dsh-plugin-kit": "^0.2.0"
15
+ },
16
+ "dsh": {
17
+ "bundle": {
18
+ "patch": "./cordis.patch.yml"
19
+ },
20
+ "client": {
21
+ "platform": "web",
22
+ "immediately": false
23
+ },
24
+ "category": "tool",
25
+ "platform": "both"
26
+ },
27
+ "meta": {
28
+ "title": "Scrapling 网页抓取",
29
+ "description": "强大的网页抓取工具:支持快速 HTTP 请求、隐身浏览器模式、JavaScript 渲染、Cloudflare 自动绕过、CSS/XPath 选择器、并发爬虫、代理轮换和多格式数据导出(JSON/CSV/XML)。"
30
+ },
31
+ "files": [
32
+ "lib/",
33
+ "client/",
34
+ "test/",
35
+ "cordis.patch.yml",
36
+ "README.md"
37
+ ],
38
+ "scripts": {
39
+ "test": "node test/plugin.test.mjs"
40
+ },
41
+ "engines": {
42
+ "node": ">=18",
43
+ "dsh": "^0.2.0-rc.2"
44
+ },
45
+ "license": "MIT",
46
+ "author": {
47
+ "name": "CapyClaw",
48
+ "url": "https://github.com/capyclaw",
49
+ "email": "support@capyclaw.dev"
50
+ },
51
+ "repository": {
52
+ "type": "git",
53
+ "url": "https://github.com/capyclaw/dsh-plugins",
54
+ "directory": "extensions/official/capyclaw-scrapling"
55
+ },
56
+ "bugs": {
57
+ "url": "https://github.com/capyclaw/dsh-plugins/issues"
58
+ },
59
+ "homepage": "https://github.com/capyclaw/dsh-plugins",
60
+ "publishConfig": {
61
+ "access": "public"
62
+ }
63
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * dsh-scrapling 插件测试
3
+ */
4
+ import { describe, it } from 'node:test'
5
+ import assert from 'node:assert'
6
+
7
+ describe('dsh-scrapling', () => {
8
+ it('应该导出 name 和 apply', async () => {
9
+ const plugin = await import('../lib/host.mjs')
10
+ assert.strictEqual(plugin.name, '@capyclaw/scrapling')
11
+ assert.strictEqual(typeof plugin.apply, 'function')
12
+ })
13
+
14
+ it('应该提供 scrapling 服务', async () => {
15
+ const plugin = await import('../lib/host.mjs')
16
+
17
+ // 模拟 Cordis 上下文
18
+ const services = {}
19
+ const ctx = {
20
+ provide(name, service) {
21
+ services[name] = service
22
+ }
23
+ }
24
+
25
+ await plugin.apply(ctx, {})
26
+
27
+ assert.ok(services.scrapling)
28
+ assert.strictEqual(typeof services.scrapling.fetch, 'function')
29
+ assert.strictEqual(typeof services.scrapling.fetchWithBrowser, 'function')
30
+ assert.strictEqual(typeof services.scrapling.parse, 'function')
31
+ })
32
+ })