spiderforce4ai 0.1.7__tar.gz → 0.1.8__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {spiderforce4ai-0.1.7 → spiderforce4ai-0.1.8}/PKG-INFO +93 -75
- {spiderforce4ai-0.1.7 → spiderforce4ai-0.1.8}/README.md +92 -74
- {spiderforce4ai-0.1.7 → spiderforce4ai-0.1.8}/pyproject.toml +1 -1
- {spiderforce4ai-0.1.7 → spiderforce4ai-0.1.8}/setup.py +1 -1
- {spiderforce4ai-0.1.7 → spiderforce4ai-0.1.8}/spiderforce4ai/__init__.py +107 -0
- {spiderforce4ai-0.1.7 → spiderforce4ai-0.1.8}/spiderforce4ai.egg-info/PKG-INFO +93 -75
- {spiderforce4ai-0.1.7 → spiderforce4ai-0.1.8}/setup.cfg +0 -0
- {spiderforce4ai-0.1.7 → spiderforce4ai-0.1.8}/spiderforce4ai.egg-info/SOURCES.txt +0 -0
- {spiderforce4ai-0.1.7 → spiderforce4ai-0.1.8}/spiderforce4ai.egg-info/dependency_links.txt +0 -0
- {spiderforce4ai-0.1.7 → spiderforce4ai-0.1.8}/spiderforce4ai.egg-info/requires.txt +0 -0
- {spiderforce4ai-0.1.7 → spiderforce4ai-0.1.8}/spiderforce4ai.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.2
|
2
2
|
Name: spiderforce4ai
|
3
|
-
Version: 0.1.
|
3
|
+
Version: 0.1.8
|
4
4
|
Summary: Python wrapper for SpiderForce4AI HTML-to-Markdown conversion service
|
5
5
|
Home-page: https://petertam.pro
|
6
6
|
Author: Piotr Tamulewicz
|
@@ -24,75 +24,73 @@ Dynamic: requires-python
|
|
24
24
|
|
25
25
|
# SpiderForce4AI Python Wrapper
|
26
26
|
|
27
|
-
A Python
|
28
|
-
|
29
|
-
## Installation
|
30
|
-
|
31
|
-
```bash
|
32
|
-
pip install spiderforce4ai
|
33
|
-
```
|
27
|
+
A Python package for web content crawling and HTML-to-Markdown conversion. Built for seamless integration with SpiderForce4AI service.
|
34
28
|
|
35
29
|
## Quick Start (Minimal Setup)
|
36
30
|
|
37
31
|
```python
|
38
32
|
from spiderforce4ai import SpiderForce4AI, CrawlConfig
|
39
33
|
|
40
|
-
# Initialize with your
|
34
|
+
# Initialize with your service URL
|
41
35
|
spider = SpiderForce4AI("http://localhost:3004")
|
42
36
|
|
43
|
-
#
|
37
|
+
# Create default config
|
44
38
|
config = CrawlConfig()
|
45
39
|
|
46
40
|
# Crawl a single URL
|
47
41
|
result = spider.crawl_url("https://example.com", config)
|
48
42
|
```
|
49
43
|
|
44
|
+
## Installation
|
45
|
+
|
46
|
+
```bash
|
47
|
+
pip install spiderforce4ai
|
48
|
+
```
|
49
|
+
|
50
50
|
## Crawling Methods
|
51
51
|
|
52
|
-
### 1. Single URL
|
52
|
+
### 1. Single URL
|
53
53
|
|
54
54
|
```python
|
55
|
-
#
|
55
|
+
# Basic usage
|
56
56
|
result = spider.crawl_url("https://example.com", config)
|
57
57
|
|
58
|
-
#
|
58
|
+
# Async version
|
59
59
|
async def crawl():
|
60
60
|
result = await spider.crawl_url_async("https://example.com", config)
|
61
61
|
```
|
62
62
|
|
63
|
-
### 2. Multiple URLs
|
63
|
+
### 2. Multiple URLs
|
64
64
|
|
65
65
|
```python
|
66
|
-
# List of URLs
|
67
66
|
urls = [
|
68
67
|
"https://example.com/page1",
|
69
|
-
"https://example.com/page2"
|
70
|
-
"https://example.com/page3"
|
68
|
+
"https://example.com/page2"
|
71
69
|
]
|
72
70
|
|
73
|
-
#
|
74
|
-
results = spider.
|
71
|
+
# Client-side parallel (using multiprocessing)
|
72
|
+
results = spider.crawl_urls_parallel(urls, config)
|
73
|
+
|
74
|
+
# Server-side parallel (single request)
|
75
|
+
results = spider.crawl_urls_server_parallel(urls, config)
|
75
76
|
|
76
|
-
#
|
77
|
+
# Async version
|
77
78
|
async def crawl():
|
78
79
|
results = await spider.crawl_urls_async(urls, config)
|
79
|
-
|
80
|
-
# Parallel (using multiprocessing)
|
81
|
-
results = spider.crawl_urls_parallel(urls, config)
|
82
80
|
```
|
83
81
|
|
84
82
|
### 3. Sitemap Crawling
|
85
83
|
|
86
84
|
```python
|
87
|
-
#
|
88
|
-
results = spider.
|
85
|
+
# Server-side parallel (recommended)
|
86
|
+
results = spider.crawl_sitemap_server_parallel("https://example.com/sitemap.xml", config)
|
87
|
+
|
88
|
+
# Client-side parallel
|
89
|
+
results = spider.crawl_sitemap_parallel("https://example.com/sitemap.xml", config)
|
89
90
|
|
90
|
-
#
|
91
|
+
# Async version
|
91
92
|
async def crawl():
|
92
93
|
results = await spider.crawl_sitemap_async("https://example.com/sitemap.xml", config)
|
93
|
-
|
94
|
-
# Parallel (using multiprocessing)
|
95
|
-
results = spider.crawl_sitemap_parallel("https://example.com/sitemap.xml", config)
|
96
94
|
```
|
97
95
|
|
98
96
|
## Configuration Options
|
@@ -100,9 +98,11 @@ results = spider.crawl_sitemap_parallel("https://example.com/sitemap.xml", confi
|
|
100
98
|
All configuration options are optional with sensible defaults:
|
101
99
|
|
102
100
|
```python
|
101
|
+
from pathlib import Path
|
102
|
+
|
103
103
|
config = CrawlConfig(
|
104
104
|
# Content Selection (all optional)
|
105
|
-
target_selector="article", # Specific element to
|
105
|
+
target_selector="article", # Specific element to extract
|
106
106
|
remove_selectors=[ # Elements to remove
|
107
107
|
".ads",
|
108
108
|
"#popup",
|
@@ -112,21 +112,21 @@ config = CrawlConfig(
|
|
112
112
|
remove_selectors_regex=["modal-\\d+"], # Regex patterns for removal
|
113
113
|
|
114
114
|
# Processing Settings
|
115
|
-
max_concurrent_requests=1, #
|
116
|
-
request_delay=0.5, # Delay between requests
|
117
|
-
timeout=30, # Request timeout
|
115
|
+
max_concurrent_requests=1, # For client-side parallel processing
|
116
|
+
request_delay=0.5, # Delay between requests (seconds)
|
117
|
+
timeout=30, # Request timeout (seconds)
|
118
118
|
|
119
119
|
# Output Settings
|
120
|
-
output_dir="
|
121
|
-
|
122
|
-
|
123
|
-
|
120
|
+
output_dir=Path("spiderforce_reports"), # Default directory for files
|
121
|
+
webhook_url="https://your-webhook.com", # Real-time notifications
|
122
|
+
webhook_timeout=10, # Webhook timeout
|
123
|
+
report_file=Path("crawl_report.json") # Final report location
|
124
124
|
)
|
125
125
|
```
|
126
126
|
|
127
127
|
## Real-World Examples
|
128
128
|
|
129
|
-
### 1. Basic
|
129
|
+
### 1. Basic Blog Crawling
|
130
130
|
|
131
131
|
```python
|
132
132
|
from spiderforce4ai import SpiderForce4AI, CrawlConfig
|
@@ -134,78 +134,77 @@ from pathlib import Path
|
|
134
134
|
|
135
135
|
spider = SpiderForce4AI("http://localhost:3004")
|
136
136
|
config = CrawlConfig(
|
137
|
+
target_selector="article.post-content",
|
137
138
|
output_dir=Path("blog_content")
|
138
139
|
)
|
139
140
|
|
140
|
-
result = spider.crawl_url("https://example.com/blog", config)
|
141
|
-
print(f"Content saved to: {result.url}.md")
|
141
|
+
result = spider.crawl_url("https://example.com/blog-post", config)
|
142
142
|
```
|
143
143
|
|
144
|
-
### 2.
|
144
|
+
### 2. Parallel Website Crawling
|
145
145
|
|
146
146
|
```python
|
147
147
|
config = CrawlConfig(
|
148
|
-
max_concurrent_requests=5,
|
149
|
-
output_dir=Path("website_content"),
|
150
148
|
remove_selectors=[
|
151
149
|
".navigation",
|
152
150
|
".footer",
|
153
151
|
".ads",
|
154
152
|
"#cookie-notice"
|
155
153
|
],
|
154
|
+
max_concurrent_requests=5,
|
155
|
+
output_dir=Path("website_content"),
|
156
156
|
webhook_url="https://your-webhook.com/endpoint"
|
157
157
|
)
|
158
158
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
159
|
+
# Using server-side parallel processing
|
160
|
+
results = spider.crawl_urls_server_parallel([
|
161
|
+
"https://example.com/page1",
|
162
|
+
"https://example.com/page2",
|
163
|
+
"https://example.com/page3"
|
164
|
+
], config)
|
163
165
|
```
|
164
166
|
|
165
|
-
### 3.
|
167
|
+
### 3. Full Sitemap Processing
|
166
168
|
|
167
169
|
```python
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
)
|
175
|
-
|
176
|
-
async with spider:
|
177
|
-
results = await spider.crawl_urls_async([
|
178
|
-
"https://example.com/1",
|
179
|
-
"https://example.com/2",
|
180
|
-
"https://example.com/3"
|
181
|
-
], config)
|
182
|
-
|
183
|
-
return results
|
170
|
+
config = CrawlConfig(
|
171
|
+
target_selector="main",
|
172
|
+
remove_selectors=[".sidebar", ".comments"],
|
173
|
+
output_dir=Path("site_content"),
|
174
|
+
report_file=Path("crawl_report.json")
|
175
|
+
)
|
184
176
|
|
185
|
-
results =
|
177
|
+
results = spider.crawl_sitemap_server_parallel(
|
178
|
+
"https://example.com/sitemap.xml",
|
179
|
+
config
|
180
|
+
)
|
186
181
|
```
|
187
182
|
|
188
183
|
## Output Structure
|
189
184
|
|
190
|
-
### 1.
|
185
|
+
### 1. Directory Layout
|
191
186
|
```
|
192
|
-
|
193
|
-
├── example-com-page1.md
|
187
|
+
spiderforce_reports/ # Default output directory
|
188
|
+
├── example-com-page1.md # Converted markdown files
|
194
189
|
├── example-com-page2.md
|
195
|
-
└── crawl_report.json
|
190
|
+
└── crawl_report.json # Crawl report
|
196
191
|
```
|
197
192
|
|
198
193
|
### 2. Markdown Files
|
199
|
-
Each
|
194
|
+
Each file is named using a slugified version of the URL:
|
195
|
+
```markdown
|
196
|
+
# Page Title
|
197
|
+
|
198
|
+
Content converted to clean markdown...
|
199
|
+
```
|
200
200
|
|
201
|
-
### 3. Report
|
201
|
+
### 3. Crawl Report
|
202
202
|
```json
|
203
203
|
{
|
204
204
|
"timestamp": "2025-02-15T10:30:00.123456",
|
205
205
|
"config": {
|
206
206
|
"target_selector": "article",
|
207
|
-
"remove_selectors": [".ads", "#popup"]
|
208
|
-
"remove_selectors_regex": ["modal-\\d+"]
|
207
|
+
"remove_selectors": [".ads", "#popup"]
|
209
208
|
},
|
210
209
|
"results": {
|
211
210
|
"successful": [
|
@@ -234,7 +233,7 @@ Each markdown file is named using a slugified version of the URL and contains th
|
|
234
233
|
```
|
235
234
|
|
236
235
|
### 4. Webhook Notifications
|
237
|
-
If configured,
|
236
|
+
If configured, real-time updates are sent for each processed URL:
|
238
237
|
```json
|
239
238
|
{
|
240
239
|
"url": "https://example.com/page1",
|
@@ -250,7 +249,7 @@ If configured, webhooks receive real-time updates in JSON format:
|
|
250
249
|
|
251
250
|
## Error Handling
|
252
251
|
|
253
|
-
The package handles various types of errors:
|
252
|
+
The package handles various types of errors gracefully:
|
254
253
|
- Network errors
|
255
254
|
- Timeout errors
|
256
255
|
- Invalid URLs
|
@@ -269,6 +268,25 @@ All errors are:
|
|
269
268
|
- Running SpiderForce4AI service
|
270
269
|
- Internet connection
|
271
270
|
|
271
|
+
## Performance Considerations
|
272
|
+
|
273
|
+
1. Server-side Parallel Processing
|
274
|
+
- Best for most cases
|
275
|
+
- Single HTTP request for multiple URLs
|
276
|
+
- Less network overhead
|
277
|
+
- Use: `crawl_urls_server_parallel()` or `crawl_sitemap_server_parallel()`
|
278
|
+
|
279
|
+
2. Client-side Parallel Processing
|
280
|
+
- Good for special cases requiring local control
|
281
|
+
- Uses Python multiprocessing
|
282
|
+
- More network overhead
|
283
|
+
- Use: `crawl_urls_parallel()` or `crawl_sitemap_parallel()`
|
284
|
+
|
285
|
+
3. Async Processing
|
286
|
+
- Best for integration with async applications
|
287
|
+
- Good for real-time processing
|
288
|
+
- Use: `crawl_url_async()`, `crawl_urls_async()`, or `crawl_sitemap_async()`
|
289
|
+
|
272
290
|
## License
|
273
291
|
|
274
292
|
MIT License
|
@@ -1,74 +1,72 @@
|
|
1
1
|
# SpiderForce4AI Python Wrapper
|
2
2
|
|
3
|
-
A Python
|
4
|
-
|
5
|
-
## Installation
|
6
|
-
|
7
|
-
```bash
|
8
|
-
pip install spiderforce4ai
|
9
|
-
```
|
3
|
+
A Python package for web content crawling and HTML-to-Markdown conversion. Built for seamless integration with SpiderForce4AI service.
|
10
4
|
|
11
5
|
## Quick Start (Minimal Setup)
|
12
6
|
|
13
7
|
```python
|
14
8
|
from spiderforce4ai import SpiderForce4AI, CrawlConfig
|
15
9
|
|
16
|
-
# Initialize with your
|
10
|
+
# Initialize with your service URL
|
17
11
|
spider = SpiderForce4AI("http://localhost:3004")
|
18
12
|
|
19
|
-
#
|
13
|
+
# Create default config
|
20
14
|
config = CrawlConfig()
|
21
15
|
|
22
16
|
# Crawl a single URL
|
23
17
|
result = spider.crawl_url("https://example.com", config)
|
24
18
|
```
|
25
19
|
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
```bash
|
23
|
+
pip install spiderforce4ai
|
24
|
+
```
|
25
|
+
|
26
26
|
## Crawling Methods
|
27
27
|
|
28
|
-
### 1. Single URL
|
28
|
+
### 1. Single URL
|
29
29
|
|
30
30
|
```python
|
31
|
-
#
|
31
|
+
# Basic usage
|
32
32
|
result = spider.crawl_url("https://example.com", config)
|
33
33
|
|
34
|
-
#
|
34
|
+
# Async version
|
35
35
|
async def crawl():
|
36
36
|
result = await spider.crawl_url_async("https://example.com", config)
|
37
37
|
```
|
38
38
|
|
39
|
-
### 2. Multiple URLs
|
39
|
+
### 2. Multiple URLs
|
40
40
|
|
41
41
|
```python
|
42
|
-
# List of URLs
|
43
42
|
urls = [
|
44
43
|
"https://example.com/page1",
|
45
|
-
"https://example.com/page2"
|
46
|
-
"https://example.com/page3"
|
44
|
+
"https://example.com/page2"
|
47
45
|
]
|
48
46
|
|
49
|
-
#
|
50
|
-
results = spider.
|
47
|
+
# Client-side parallel (using multiprocessing)
|
48
|
+
results = spider.crawl_urls_parallel(urls, config)
|
49
|
+
|
50
|
+
# Server-side parallel (single request)
|
51
|
+
results = spider.crawl_urls_server_parallel(urls, config)
|
51
52
|
|
52
|
-
#
|
53
|
+
# Async version
|
53
54
|
async def crawl():
|
54
55
|
results = await spider.crawl_urls_async(urls, config)
|
55
|
-
|
56
|
-
# Parallel (using multiprocessing)
|
57
|
-
results = spider.crawl_urls_parallel(urls, config)
|
58
56
|
```
|
59
57
|
|
60
58
|
### 3. Sitemap Crawling
|
61
59
|
|
62
60
|
```python
|
63
|
-
#
|
64
|
-
results = spider.
|
61
|
+
# Server-side parallel (recommended)
|
62
|
+
results = spider.crawl_sitemap_server_parallel("https://example.com/sitemap.xml", config)
|
63
|
+
|
64
|
+
# Client-side parallel
|
65
|
+
results = spider.crawl_sitemap_parallel("https://example.com/sitemap.xml", config)
|
65
66
|
|
66
|
-
#
|
67
|
+
# Async version
|
67
68
|
async def crawl():
|
68
69
|
results = await spider.crawl_sitemap_async("https://example.com/sitemap.xml", config)
|
69
|
-
|
70
|
-
# Parallel (using multiprocessing)
|
71
|
-
results = spider.crawl_sitemap_parallel("https://example.com/sitemap.xml", config)
|
72
70
|
```
|
73
71
|
|
74
72
|
## Configuration Options
|
@@ -76,9 +74,11 @@ results = spider.crawl_sitemap_parallel("https://example.com/sitemap.xml", confi
|
|
76
74
|
All configuration options are optional with sensible defaults:
|
77
75
|
|
78
76
|
```python
|
77
|
+
from pathlib import Path
|
78
|
+
|
79
79
|
config = CrawlConfig(
|
80
80
|
# Content Selection (all optional)
|
81
|
-
target_selector="article", # Specific element to
|
81
|
+
target_selector="article", # Specific element to extract
|
82
82
|
remove_selectors=[ # Elements to remove
|
83
83
|
".ads",
|
84
84
|
"#popup",
|
@@ -88,21 +88,21 @@ config = CrawlConfig(
|
|
88
88
|
remove_selectors_regex=["modal-\\d+"], # Regex patterns for removal
|
89
89
|
|
90
90
|
# Processing Settings
|
91
|
-
max_concurrent_requests=1, #
|
92
|
-
request_delay=0.5, # Delay between requests
|
93
|
-
timeout=30, # Request timeout
|
91
|
+
max_concurrent_requests=1, # For client-side parallel processing
|
92
|
+
request_delay=0.5, # Delay between requests (seconds)
|
93
|
+
timeout=30, # Request timeout (seconds)
|
94
94
|
|
95
95
|
# Output Settings
|
96
|
-
output_dir="
|
97
|
-
|
98
|
-
|
99
|
-
|
96
|
+
output_dir=Path("spiderforce_reports"), # Default directory for files
|
97
|
+
webhook_url="https://your-webhook.com", # Real-time notifications
|
98
|
+
webhook_timeout=10, # Webhook timeout
|
99
|
+
report_file=Path("crawl_report.json") # Final report location
|
100
100
|
)
|
101
101
|
```
|
102
102
|
|
103
103
|
## Real-World Examples
|
104
104
|
|
105
|
-
### 1. Basic
|
105
|
+
### 1. Basic Blog Crawling
|
106
106
|
|
107
107
|
```python
|
108
108
|
from spiderforce4ai import SpiderForce4AI, CrawlConfig
|
@@ -110,78 +110,77 @@ from pathlib import Path
|
|
110
110
|
|
111
111
|
spider = SpiderForce4AI("http://localhost:3004")
|
112
112
|
config = CrawlConfig(
|
113
|
+
target_selector="article.post-content",
|
113
114
|
output_dir=Path("blog_content")
|
114
115
|
)
|
115
116
|
|
116
|
-
result = spider.crawl_url("https://example.com/blog", config)
|
117
|
-
print(f"Content saved to: {result.url}.md")
|
117
|
+
result = spider.crawl_url("https://example.com/blog-post", config)
|
118
118
|
```
|
119
119
|
|
120
|
-
### 2.
|
120
|
+
### 2. Parallel Website Crawling
|
121
121
|
|
122
122
|
```python
|
123
123
|
config = CrawlConfig(
|
124
|
-
max_concurrent_requests=5,
|
125
|
-
output_dir=Path("website_content"),
|
126
124
|
remove_selectors=[
|
127
125
|
".navigation",
|
128
126
|
".footer",
|
129
127
|
".ads",
|
130
128
|
"#cookie-notice"
|
131
129
|
],
|
130
|
+
max_concurrent_requests=5,
|
131
|
+
output_dir=Path("website_content"),
|
132
132
|
webhook_url="https://your-webhook.com/endpoint"
|
133
133
|
)
|
134
134
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
135
|
+
# Using server-side parallel processing
|
136
|
+
results = spider.crawl_urls_server_parallel([
|
137
|
+
"https://example.com/page1",
|
138
|
+
"https://example.com/page2",
|
139
|
+
"https://example.com/page3"
|
140
|
+
], config)
|
139
141
|
```
|
140
142
|
|
141
|
-
### 3.
|
143
|
+
### 3. Full Sitemap Processing
|
142
144
|
|
143
145
|
```python
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
)
|
151
|
-
|
152
|
-
async with spider:
|
153
|
-
results = await spider.crawl_urls_async([
|
154
|
-
"https://example.com/1",
|
155
|
-
"https://example.com/2",
|
156
|
-
"https://example.com/3"
|
157
|
-
], config)
|
158
|
-
|
159
|
-
return results
|
146
|
+
config = CrawlConfig(
|
147
|
+
target_selector="main",
|
148
|
+
remove_selectors=[".sidebar", ".comments"],
|
149
|
+
output_dir=Path("site_content"),
|
150
|
+
report_file=Path("crawl_report.json")
|
151
|
+
)
|
160
152
|
|
161
|
-
results =
|
153
|
+
results = spider.crawl_sitemap_server_parallel(
|
154
|
+
"https://example.com/sitemap.xml",
|
155
|
+
config
|
156
|
+
)
|
162
157
|
```
|
163
158
|
|
164
159
|
## Output Structure
|
165
160
|
|
166
|
-
### 1.
|
161
|
+
### 1. Directory Layout
|
167
162
|
```
|
168
|
-
|
169
|
-
├── example-com-page1.md
|
163
|
+
spiderforce_reports/ # Default output directory
|
164
|
+
├── example-com-page1.md # Converted markdown files
|
170
165
|
├── example-com-page2.md
|
171
|
-
└── crawl_report.json
|
166
|
+
└── crawl_report.json # Crawl report
|
172
167
|
```
|
173
168
|
|
174
169
|
### 2. Markdown Files
|
175
|
-
Each
|
170
|
+
Each file is named using a slugified version of the URL:
|
171
|
+
```markdown
|
172
|
+
# Page Title
|
173
|
+
|
174
|
+
Content converted to clean markdown...
|
175
|
+
```
|
176
176
|
|
177
|
-
### 3. Report
|
177
|
+
### 3. Crawl Report
|
178
178
|
```json
|
179
179
|
{
|
180
180
|
"timestamp": "2025-02-15T10:30:00.123456",
|
181
181
|
"config": {
|
182
182
|
"target_selector": "article",
|
183
|
-
"remove_selectors": [".ads", "#popup"]
|
184
|
-
"remove_selectors_regex": ["modal-\\d+"]
|
183
|
+
"remove_selectors": [".ads", "#popup"]
|
185
184
|
},
|
186
185
|
"results": {
|
187
186
|
"successful": [
|
@@ -210,7 +209,7 @@ Each markdown file is named using a slugified version of the URL and contains th
|
|
210
209
|
```
|
211
210
|
|
212
211
|
### 4. Webhook Notifications
|
213
|
-
If configured,
|
212
|
+
If configured, real-time updates are sent for each processed URL:
|
214
213
|
```json
|
215
214
|
{
|
216
215
|
"url": "https://example.com/page1",
|
@@ -226,7 +225,7 @@ If configured, webhooks receive real-time updates in JSON format:
|
|
226
225
|
|
227
226
|
## Error Handling
|
228
227
|
|
229
|
-
The package handles various types of errors:
|
228
|
+
The package handles various types of errors gracefully:
|
230
229
|
- Network errors
|
231
230
|
- Timeout errors
|
232
231
|
- Invalid URLs
|
@@ -245,6 +244,25 @@ All errors are:
|
|
245
244
|
- Running SpiderForce4AI service
|
246
245
|
- Internet connection
|
247
246
|
|
247
|
+
## Performance Considerations
|
248
|
+
|
249
|
+
1. Server-side Parallel Processing
|
250
|
+
- Best for most cases
|
251
|
+
- Single HTTP request for multiple URLs
|
252
|
+
- Less network overhead
|
253
|
+
- Use: `crawl_urls_server_parallel()` or `crawl_sitemap_server_parallel()`
|
254
|
+
|
255
|
+
2. Client-side Parallel Processing
|
256
|
+
- Good for special cases requiring local control
|
257
|
+
- Uses Python multiprocessing
|
258
|
+
- More network overhead
|
259
|
+
- Use: `crawl_urls_parallel()` or `crawl_sitemap_parallel()`
|
260
|
+
|
261
|
+
3. Async Processing
|
262
|
+
- Best for integration with async applications
|
263
|
+
- Good for real-time processing
|
264
|
+
- Use: `crawl_url_async()`, `crawl_urls_async()`, or `crawl_sitemap_async()`
|
265
|
+
|
248
266
|
## License
|
249
267
|
|
250
268
|
MIT License
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
4
4
|
|
5
5
|
[project]
|
6
6
|
name = "spiderforce4ai"
|
7
|
-
version = "0.1.
|
7
|
+
version = "0.1.8"
|
8
8
|
description = "Python wrapper for SpiderForce4AI HTML-to-Markdown conversion service"
|
9
9
|
readme = "README.md"
|
10
10
|
authors = [{name = "Piotr Tamulewicz", email = "pt@petertam.pro"}]
|
@@ -196,6 +196,113 @@ class SpiderForce4AI:
|
|
196
196
|
await f.write(markdown)
|
197
197
|
return filepath
|
198
198
|
|
199
|
+
|
200
|
+
|
201
|
+
def crawl_sitemap_server_parallel(self, sitemap_url: str, config: CrawlConfig) -> List[CrawlResult]:
|
202
|
+
"""
|
203
|
+
Crawl sitemap URLs using server-side parallel processing.
|
204
|
+
"""
|
205
|
+
print(f"Fetching sitemap from {sitemap_url}...")
|
206
|
+
|
207
|
+
# Fetch sitemap
|
208
|
+
try:
|
209
|
+
response = requests.get(sitemap_url, timeout=config.timeout)
|
210
|
+
response.raise_for_status()
|
211
|
+
sitemap_text = response.text
|
212
|
+
except Exception as e:
|
213
|
+
print(f"Error fetching sitemap: {str(e)}")
|
214
|
+
raise
|
215
|
+
|
216
|
+
# Parse sitemap
|
217
|
+
try:
|
218
|
+
root = ET.fromstring(sitemap_text)
|
219
|
+
namespace = {'ns': root.tag.split('}')[0].strip('{')}
|
220
|
+
urls = [loc.text for loc in root.findall('.//ns:loc', namespace)]
|
221
|
+
print(f"Found {len(urls)} URLs in sitemap")
|
222
|
+
except Exception as e:
|
223
|
+
print(f"Error parsing sitemap: {str(e)}")
|
224
|
+
raise
|
225
|
+
|
226
|
+
# Process URLs using server-side parallel endpoint
|
227
|
+
return self.crawl_urls_server_parallel(urls, config)
|
228
|
+
|
229
|
+
|
230
|
+
def crawl_urls_server_parallel(self, urls: List[str], config: CrawlConfig) -> List[CrawlResult]:
|
231
|
+
"""
|
232
|
+
Crawl multiple URLs using server-side parallel processing.
|
233
|
+
This uses the /convert_parallel endpoint which handles parallelization on the server.
|
234
|
+
"""
|
235
|
+
print(f"Sending {len(urls)} URLs for parallel processing...")
|
236
|
+
|
237
|
+
try:
|
238
|
+
endpoint = f"{self.base_url}/convert_parallel"
|
239
|
+
|
240
|
+
# Prepare payload
|
241
|
+
payload = {
|
242
|
+
"urls": urls,
|
243
|
+
**config.to_dict()
|
244
|
+
}
|
245
|
+
|
246
|
+
# Send request
|
247
|
+
response = requests.post(
|
248
|
+
endpoint,
|
249
|
+
json=payload,
|
250
|
+
timeout=config.timeout
|
251
|
+
)
|
252
|
+
response.raise_for_status()
|
253
|
+
|
254
|
+
# Process results
|
255
|
+
results = []
|
256
|
+
server_results = response.json() # Assuming server returns JSON array of results
|
257
|
+
|
258
|
+
for url_result in server_results:
|
259
|
+
result = CrawlResult(
|
260
|
+
url=url_result["url"],
|
261
|
+
status=url_result.get("status", "failed"),
|
262
|
+
markdown=url_result.get("markdown"),
|
263
|
+
error=url_result.get("error"),
|
264
|
+
config=config.to_dict()
|
265
|
+
)
|
266
|
+
|
267
|
+
# Save markdown if successful and output dir is configured
|
268
|
+
if result.status == "success" and config.output_dir and result.markdown:
|
269
|
+
filepath = config.output_dir / f"{slugify(result.url)}.md"
|
270
|
+
with open(filepath, 'w', encoding='utf-8') as f:
|
271
|
+
f.write(result.markdown)
|
272
|
+
|
273
|
+
# Send webhook if configured
|
274
|
+
if config.webhook_url:
|
275
|
+
_send_webhook_sync(result, config)
|
276
|
+
|
277
|
+
results.append(result)
|
278
|
+
|
279
|
+
# Save report if configured
|
280
|
+
if config.report_file:
|
281
|
+
self._save_report_sync(results, config)
|
282
|
+
print(f"\nReport saved to: {config.report_file}")
|
283
|
+
|
284
|
+
# Print summary
|
285
|
+
successful = len([r for r in results if r.status == "success"])
|
286
|
+
failed = len([r for r in results if r.status == "failed"])
|
287
|
+
print(f"\nParallel processing completed:")
|
288
|
+
print(f"✓ Successful: {successful}")
|
289
|
+
print(f"✗ Failed: {failed}")
|
290
|
+
|
291
|
+
return results
|
292
|
+
|
293
|
+
except Exception as e:
|
294
|
+
print(f"Error during parallel processing: {str(e)}")
|
295
|
+
# Create failed results for all URLs
|
296
|
+
return [
|
297
|
+
CrawlResult(
|
298
|
+
url=url,
|
299
|
+
status="failed",
|
300
|
+
error=str(e),
|
301
|
+
config=config.to_dict()
|
302
|
+
) for url in urls
|
303
|
+
]
|
304
|
+
|
305
|
+
|
199
306
|
async def _send_webhook(self, result: CrawlResult, config: CrawlConfig):
|
200
307
|
"""Send webhook with crawl results."""
|
201
308
|
if not config.webhook_url:
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.2
|
2
2
|
Name: spiderforce4ai
|
3
|
-
Version: 0.1.
|
3
|
+
Version: 0.1.8
|
4
4
|
Summary: Python wrapper for SpiderForce4AI HTML-to-Markdown conversion service
|
5
5
|
Home-page: https://petertam.pro
|
6
6
|
Author: Piotr Tamulewicz
|
@@ -24,75 +24,73 @@ Dynamic: requires-python
|
|
24
24
|
|
25
25
|
# SpiderForce4AI Python Wrapper
|
26
26
|
|
27
|
-
A Python
|
28
|
-
|
29
|
-
## Installation
|
30
|
-
|
31
|
-
```bash
|
32
|
-
pip install spiderforce4ai
|
33
|
-
```
|
27
|
+
A Python package for web content crawling and HTML-to-Markdown conversion. Built for seamless integration with SpiderForce4AI service.
|
34
28
|
|
35
29
|
## Quick Start (Minimal Setup)
|
36
30
|
|
37
31
|
```python
|
38
32
|
from spiderforce4ai import SpiderForce4AI, CrawlConfig
|
39
33
|
|
40
|
-
# Initialize with your
|
34
|
+
# Initialize with your service URL
|
41
35
|
spider = SpiderForce4AI("http://localhost:3004")
|
42
36
|
|
43
|
-
#
|
37
|
+
# Create default config
|
44
38
|
config = CrawlConfig()
|
45
39
|
|
46
40
|
# Crawl a single URL
|
47
41
|
result = spider.crawl_url("https://example.com", config)
|
48
42
|
```
|
49
43
|
|
44
|
+
## Installation
|
45
|
+
|
46
|
+
```bash
|
47
|
+
pip install spiderforce4ai
|
48
|
+
```
|
49
|
+
|
50
50
|
## Crawling Methods
|
51
51
|
|
52
|
-
### 1. Single URL
|
52
|
+
### 1. Single URL
|
53
53
|
|
54
54
|
```python
|
55
|
-
#
|
55
|
+
# Basic usage
|
56
56
|
result = spider.crawl_url("https://example.com", config)
|
57
57
|
|
58
|
-
#
|
58
|
+
# Async version
|
59
59
|
async def crawl():
|
60
60
|
result = await spider.crawl_url_async("https://example.com", config)
|
61
61
|
```
|
62
62
|
|
63
|
-
### 2. Multiple URLs
|
63
|
+
### 2. Multiple URLs
|
64
64
|
|
65
65
|
```python
|
66
|
-
# List of URLs
|
67
66
|
urls = [
|
68
67
|
"https://example.com/page1",
|
69
|
-
"https://example.com/page2"
|
70
|
-
"https://example.com/page3"
|
68
|
+
"https://example.com/page2"
|
71
69
|
]
|
72
70
|
|
73
|
-
#
|
74
|
-
results = spider.
|
71
|
+
# Client-side parallel (using multiprocessing)
|
72
|
+
results = spider.crawl_urls_parallel(urls, config)
|
73
|
+
|
74
|
+
# Server-side parallel (single request)
|
75
|
+
results = spider.crawl_urls_server_parallel(urls, config)
|
75
76
|
|
76
|
-
#
|
77
|
+
# Async version
|
77
78
|
async def crawl():
|
78
79
|
results = await spider.crawl_urls_async(urls, config)
|
79
|
-
|
80
|
-
# Parallel (using multiprocessing)
|
81
|
-
results = spider.crawl_urls_parallel(urls, config)
|
82
80
|
```
|
83
81
|
|
84
82
|
### 3. Sitemap Crawling
|
85
83
|
|
86
84
|
```python
|
87
|
-
#
|
88
|
-
results = spider.
|
85
|
+
# Server-side parallel (recommended)
|
86
|
+
results = spider.crawl_sitemap_server_parallel("https://example.com/sitemap.xml", config)
|
87
|
+
|
88
|
+
# Client-side parallel
|
89
|
+
results = spider.crawl_sitemap_parallel("https://example.com/sitemap.xml", config)
|
89
90
|
|
90
|
-
#
|
91
|
+
# Async version
|
91
92
|
async def crawl():
|
92
93
|
results = await spider.crawl_sitemap_async("https://example.com/sitemap.xml", config)
|
93
|
-
|
94
|
-
# Parallel (using multiprocessing)
|
95
|
-
results = spider.crawl_sitemap_parallel("https://example.com/sitemap.xml", config)
|
96
94
|
```
|
97
95
|
|
98
96
|
## Configuration Options
|
@@ -100,9 +98,11 @@ results = spider.crawl_sitemap_parallel("https://example.com/sitemap.xml", confi
|
|
100
98
|
All configuration options are optional with sensible defaults:
|
101
99
|
|
102
100
|
```python
|
101
|
+
from pathlib import Path
|
102
|
+
|
103
103
|
config = CrawlConfig(
|
104
104
|
# Content Selection (all optional)
|
105
|
-
target_selector="article", # Specific element to
|
105
|
+
target_selector="article", # Specific element to extract
|
106
106
|
remove_selectors=[ # Elements to remove
|
107
107
|
".ads",
|
108
108
|
"#popup",
|
@@ -112,21 +112,21 @@ config = CrawlConfig(
|
|
112
112
|
remove_selectors_regex=["modal-\\d+"], # Regex patterns for removal
|
113
113
|
|
114
114
|
# Processing Settings
|
115
|
-
max_concurrent_requests=1, #
|
116
|
-
request_delay=0.5, # Delay between requests
|
117
|
-
timeout=30, # Request timeout
|
115
|
+
max_concurrent_requests=1, # For client-side parallel processing
|
116
|
+
request_delay=0.5, # Delay between requests (seconds)
|
117
|
+
timeout=30, # Request timeout (seconds)
|
118
118
|
|
119
119
|
# Output Settings
|
120
|
-
output_dir="
|
121
|
-
|
122
|
-
|
123
|
-
|
120
|
+
output_dir=Path("spiderforce_reports"), # Default directory for files
|
121
|
+
webhook_url="https://your-webhook.com", # Real-time notifications
|
122
|
+
webhook_timeout=10, # Webhook timeout
|
123
|
+
report_file=Path("crawl_report.json") # Final report location
|
124
124
|
)
|
125
125
|
```
|
126
126
|
|
127
127
|
## Real-World Examples
|
128
128
|
|
129
|
-
### 1. Basic
|
129
|
+
### 1. Basic Blog Crawling
|
130
130
|
|
131
131
|
```python
|
132
132
|
from spiderforce4ai import SpiderForce4AI, CrawlConfig
|
@@ -134,78 +134,77 @@ from pathlib import Path
|
|
134
134
|
|
135
135
|
spider = SpiderForce4AI("http://localhost:3004")
|
136
136
|
config = CrawlConfig(
|
137
|
+
target_selector="article.post-content",
|
137
138
|
output_dir=Path("blog_content")
|
138
139
|
)
|
139
140
|
|
140
|
-
result = spider.crawl_url("https://example.com/blog", config)
|
141
|
-
print(f"Content saved to: {result.url}.md")
|
141
|
+
result = spider.crawl_url("https://example.com/blog-post", config)
|
142
142
|
```
|
143
143
|
|
144
|
-
### 2.
|
144
|
+
### 2. Parallel Website Crawling
|
145
145
|
|
146
146
|
```python
|
147
147
|
config = CrawlConfig(
|
148
|
-
max_concurrent_requests=5,
|
149
|
-
output_dir=Path("website_content"),
|
150
148
|
remove_selectors=[
|
151
149
|
".navigation",
|
152
150
|
".footer",
|
153
151
|
".ads",
|
154
152
|
"#cookie-notice"
|
155
153
|
],
|
154
|
+
max_concurrent_requests=5,
|
155
|
+
output_dir=Path("website_content"),
|
156
156
|
webhook_url="https://your-webhook.com/endpoint"
|
157
157
|
)
|
158
158
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
159
|
+
# Using server-side parallel processing
|
160
|
+
results = spider.crawl_urls_server_parallel([
|
161
|
+
"https://example.com/page1",
|
162
|
+
"https://example.com/page2",
|
163
|
+
"https://example.com/page3"
|
164
|
+
], config)
|
163
165
|
```
|
164
166
|
|
165
|
-
### 3.
|
167
|
+
### 3. Full Sitemap Processing
|
166
168
|
|
167
169
|
```python
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
)
|
175
|
-
|
176
|
-
async with spider:
|
177
|
-
results = await spider.crawl_urls_async([
|
178
|
-
"https://example.com/1",
|
179
|
-
"https://example.com/2",
|
180
|
-
"https://example.com/3"
|
181
|
-
], config)
|
182
|
-
|
183
|
-
return results
|
170
|
+
config = CrawlConfig(
|
171
|
+
target_selector="main",
|
172
|
+
remove_selectors=[".sidebar", ".comments"],
|
173
|
+
output_dir=Path("site_content"),
|
174
|
+
report_file=Path("crawl_report.json")
|
175
|
+
)
|
184
176
|
|
185
|
-
results =
|
177
|
+
results = spider.crawl_sitemap_server_parallel(
|
178
|
+
"https://example.com/sitemap.xml",
|
179
|
+
config
|
180
|
+
)
|
186
181
|
```
|
187
182
|
|
188
183
|
## Output Structure
|
189
184
|
|
190
|
-
### 1.
|
185
|
+
### 1. Directory Layout
|
191
186
|
```
|
192
|
-
|
193
|
-
├── example-com-page1.md
|
187
|
+
spiderforce_reports/ # Default output directory
|
188
|
+
├── example-com-page1.md # Converted markdown files
|
194
189
|
├── example-com-page2.md
|
195
|
-
└── crawl_report.json
|
190
|
+
└── crawl_report.json # Crawl report
|
196
191
|
```
|
197
192
|
|
198
193
|
### 2. Markdown Files
|
199
|
-
Each
|
194
|
+
Each file is named using a slugified version of the URL:
|
195
|
+
```markdown
|
196
|
+
# Page Title
|
197
|
+
|
198
|
+
Content converted to clean markdown...
|
199
|
+
```
|
200
200
|
|
201
|
-
### 3. Report
|
201
|
+
### 3. Crawl Report
|
202
202
|
```json
|
203
203
|
{
|
204
204
|
"timestamp": "2025-02-15T10:30:00.123456",
|
205
205
|
"config": {
|
206
206
|
"target_selector": "article",
|
207
|
-
"remove_selectors": [".ads", "#popup"]
|
208
|
-
"remove_selectors_regex": ["modal-\\d+"]
|
207
|
+
"remove_selectors": [".ads", "#popup"]
|
209
208
|
},
|
210
209
|
"results": {
|
211
210
|
"successful": [
|
@@ -234,7 +233,7 @@ Each markdown file is named using a slugified version of the URL and contains th
|
|
234
233
|
```
|
235
234
|
|
236
235
|
### 4. Webhook Notifications
|
237
|
-
If configured,
|
236
|
+
If configured, real-time updates are sent for each processed URL:
|
238
237
|
```json
|
239
238
|
{
|
240
239
|
"url": "https://example.com/page1",
|
@@ -250,7 +249,7 @@ If configured, webhooks receive real-time updates in JSON format:
|
|
250
249
|
|
251
250
|
## Error Handling
|
252
251
|
|
253
|
-
The package handles various types of errors:
|
252
|
+
The package handles various types of errors gracefully:
|
254
253
|
- Network errors
|
255
254
|
- Timeout errors
|
256
255
|
- Invalid URLs
|
@@ -269,6 +268,25 @@ All errors are:
|
|
269
268
|
- Running SpiderForce4AI service
|
270
269
|
- Internet connection
|
271
270
|
|
271
|
+
## Performance Considerations
|
272
|
+
|
273
|
+
1. Server-side Parallel Processing
|
274
|
+
- Best for most cases
|
275
|
+
- Single HTTP request for multiple URLs
|
276
|
+
- Less network overhead
|
277
|
+
- Use: `crawl_urls_server_parallel()` or `crawl_sitemap_server_parallel()`
|
278
|
+
|
279
|
+
2. Client-side Parallel Processing
|
280
|
+
- Good for special cases requiring local control
|
281
|
+
- Uses Python multiprocessing
|
282
|
+
- More network overhead
|
283
|
+
- Use: `crawl_urls_parallel()` or `crawl_sitemap_parallel()`
|
284
|
+
|
285
|
+
3. Async Processing
|
286
|
+
- Best for integration with async applications
|
287
|
+
- Good for real-time processing
|
288
|
+
- Use: `crawl_url_async()`, `crawl_urls_async()`, or `crawl_sitemap_async()`
|
289
|
+
|
272
290
|
## License
|
273
291
|
|
274
292
|
MIT License
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|