pagesift 0.81.4

A single-binary, Firecrawl-compatible web scraping engine that turns pages into clean Markdown.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use super::*;

impl Scraper {
    pub(super) async fn fetch_and_extract(
        &self,
        url: &str,
        opts: &ScrapeOptions,
        mode: RobotsMode,
    ) -> Result<Document, ApiError> {
        let started = Instant::now();
        if opts.discover_scripts
            && (opts.engine.as_deref() != Some("http")
                || !opts.wants("links")
                || opts.egress.is_some()
                || !opts.headers.is_empty()
                || opts.if_none_match.is_some()
                || opts.if_modified_since.is_some())
        {
            return Err(ApiError::unsupported(
                "discoverScripts requires HTTP engine, links, and no custom headers/egress/conditional request",
            ));
        }
        let choice = match opts.engine.as_deref() {
            Some("http") => EngineChoice::Http,
            Some("browser") => EngineChoice::Browser,
            _ => EngineChoice::Auto,
        };
        let mut timings = BTreeMap::new();
        let mut warnings = Vec::new();

        if choice == EngineChoice::Browser && opts.egress.is_some() {
            return Err(ApiError::unsupported(
                "egress applies to HTTP fetching only; browser rendering does not use it",
            ));
        }
        if choice == EngineChoice::Browser {
            return self
                .render(
                    url,
                    opts,
                    mode,
                    Progress {
                        started,
                        timings,
                        warnings,
                    },
                    None,
                    None,
                )
                .await;
        }

        // 并发名额一直拿到提取结束:已下载、待提取的页面数不超过全局并发,机器忙时内存不会堆积
        let egress = opts.egress.as_deref().or_else(|| {
            opts.proxy
                .as_deref()
                .filter(|p| !matches!(*p, "basic" | "auto"))
        });
        let fetch = self
            .policy
            .fetch_held_via(fetch_request(url, opts)?, mode, egress);
        let (mut resp, permit) = match request_deadline(opts, started) {
            Some(d) => tokio::time::timeout_at(d.into(), fetch)
                .await
                .map_err(|_| ApiError::new(ErrorCode::Timeout, "request deadline passed"))?,
            None => fetch.await,
        }
        .map_err(policy_error)?;
        let queued = permit.waited();
        timings.insert("queue".into(), ms(queued));
        if queued >= Duration::from_millis(10) {
            if permit.robots_crawl_delay() {
                warnings.push(format!(
                    "request waited {:.0} ms in the rate-limit/admission queue; robots.txt Crawl-delay applies with an effective {:.0} ms interval",
                    ms(queued),
                    ms(permit.interval())
                ));
            } else {
                warnings.push(format!(
                    "request waited {:.0} ms in the rate-limit/admission queue",
                    ms(queued)
                ));
            }
        }
        // Keep the admission permit optional: auto-render releases it before
        // handing the page to the browser, but if the browser upgrade times out
        // and we fall back to HTTP, the permit is already gone and the rest of
        // the HTTP path must not try to drop it twice.
        let mut permit = Some(permit);
        timings.insert("fetch".into(), ms(resp.timings.total));
        let mut ext = PagesiftMeta {
            redirect_chain: resp
                .redirect_chain
                .iter()
                .map(|h| h.url.to_string())
                .collect(),
            etag: resp.etag().map(str::to_string),
            last_modified: resp.last_modified().map(str::to_string),
            not_modified: resp.not_modified,
            engine: Some("http".into()),
            ..Default::default()
        };
        // 网络层的解码提示(多层压缩、声明与实际不符、部分解压失败)原样带出去
        warnings.extend(resp.warnings.iter().cloned());
        if resp.truncated {
            warnings.push("response body exceeded the size limit and was truncated".into());
        }
        let mut meta = Metadata {
            url: Some(resp.final_url.to_string()),
            source_url: Some(url.to_string()),
            status_code: resp.status,
            content_type: resp.content_type().map(str::to_string),
            proxy_used: egress.unwrap_or("basic").into(),
            ..Default::default()
        };
        if resp.not_modified {
            if opts.if_none_match.is_none() && opts.if_modified_since.is_none() {
                // 没发条件请求却拿到 304:服务器行为异常,这里也没有缓存可用。
                warnings.push(
                    "not_modified: HTTP 304 for an unconditional request; no content was returned"
                        .into(),
                );
            }
            ext.timings = timings;
            ext.warnings = warnings;
            meta.pagesift = Some(ext);
            return Ok(Document {
                metadata: meta,
                ..Default::default()
            });
        }

        // Word、Excel、PowerPoint、EPUB、CSV:内容、Content-Type、网址的扩展名一起判断
        if let Some(format) = office_format(&resp.body, resp.final_url.path(), resp.content_type())
        {
            let progress = Progress {
                started,
                timings,
                warnings,
            };
            let deadline = request_deadline(opts, started);
            let input = office::OfficeInput {
                content_type: resp.content_type().map(str::to_owned),
                bytes: resp.body,
                format,
            };
            return self
                .office_document(input, opts, (meta, ext), progress, deadline)
                .await;
        }

        if let Some(kind) = unsupported_content_type(resp.content_type(), &resp.body) {
            return Err(ApiError::unsupported(format!(
                "unsupported content type `{kind}`: pagesift extracts HTML/text; use `pagesift download` or `media-extract` for binary media"
            )));
        }

        if is_pdf(&resp.body, resp.content_type()) {
            let progress = Progress {
                started,
                timings,
                warnings,
            };
            return self
                .pdf_document(
                    resp.body,
                    opts,
                    (meta, ext),
                    progress,
                    request_deadline(opts, started),
                )
                .await;
        }

        if let Some(markdown) = json_markdown(resp.content_type(), &resp.body) {
            return Ok(finish(
                Document {
                    markdown: opts.wants("markdown").then_some(markdown),
                    ..Default::default()
                },
                meta,
                ext,
                timings,
                warnings,
                None,
                false,
                started,
            ));
        }

        let navigation = if opts.wants("links") {
            crate::discovery::header_links(
                &resp.final_url,
                resp.headers
                    .get_all("link")
                    .iter()
                    .filter_map(|h| h.to_str().ok()),
            )
        } else {
            Vec::new()
        };
        let decoded = resp.take_text();
        if let Some(warning) = decoded.warning() {
            warnings.push(warning);
        }
        let html = decoded.text;
        let t = Instant::now();
        let parse_stage = self
            .policy
            .fetcher()
            .acquire_stage(pagesift_net::NetworkStage::Parse)
            .await;
        let (doc, html) = match process_html(html, &resp.final_url, Some(resp.status), opts).await {
            Ok(result) => {
                parse_stage.finish(pagesift_net::StageOutcome::Success);
                result
            }
            Err(error) => {
                parse_stage.finish(pagesift_net::StageOutcome::DeterministicFailure);
                return Err(error);
            }
        };
        timings.insert("extract".into(), ms(t.elapsed()));
        if doc.metadata.video_fallback {
            warnings.push(
                "video_metadata: no readable body text; used the page's embedded video metadata"
                    .into(),
            );
        }
        // 拦截页如实报告,不升级到浏览器:用真浏览器去过挑战页就是绕过
        check_barrier(&doc)?;

        let http_chars = text_weight(&doc.markdown);
        // 短页面不一定需要浏览器:没有脚本、HTML 本身也很小的静态页,渲染只会白抓一次
        // (crawl 时就是种子页被请求两次)。真正需要的是带客户端代码、或 HTML 很大却几乎
        // 没抽出文字的页面。
        let worth_rendering = html.len() >= 4096 || has_script_tag(&html);
        if choice == EngineChoice::Auto && needs_render(&doc) && worth_rendering {
            if opts.egress.is_some() {
                // 浏览器不走指定的出口,用它渲染出口地址就不一致了
                warnings.push("render_skipped_egress".into());
            } else if self.learner.skip_render(url) && !has_client_code(&html) {
                // 同路径族的静态经验不能覆盖带客户端代码的短页面。
                warnings.push("render_skipped_learned".into());
            } else if self.has_browser().await {
                // HTTP extraction no longer needs its admission slots. Rendering
                // takes a fresh slot below; retaining both can deadlock a batch
                // when every per-host slot is held by an auto-upgrade.
                permit.take();
                // Auto-upgrade is opportunistic: the HTTP document is already a
                // valid result. The probe compares the *extracted* content of
                // the live DOM with what HTTP already produced, so navigation
                // bars and ad text cannot masquerade as a gain. The budget and
                // probe time come from observed render distributions, not from
                // fixed seconds.
                let probe_opts = opts.clone();
                let probe_base = resp.final_url.clone();
                let baseline = http_chars;
                let probe = Arc::new(move |html: &str| {
                    let copts = pagesift_content::Options {
                        base_url: Some(probe_base.clone()),
                        only_main_content: probe_opts.only_main_content,
                        status: None,
                        detect_barrier: false,
                        ..Default::default()
                    };
                    let doc = pagesift_content::process(html, &copts);
                    text_weight(&doc.markdown) <= baseline
                });
                let early_stop = pagesift_cdp::EarlyStop {
                    baseline_chars: http_chars as u64,
                    cap_navigation: http_chars == 0
                        && html.len() < 4096
                        && has_client_redirect(&html),
                    http_elapsed: resp.timings.total,
                    after: self.render_stats.early_stop_after(),
                    probe,
                };
                let budget = self.render_stats.budget();
                let progress = Progress {
                    started,
                    timings: timings.clone(),
                    warnings: warnings.clone(),
                };
                let render = self.render(
                    url,
                    opts,
                    mode,
                    progress,
                    Some(http_chars),
                    Some(early_stop),
                );
                let outcome = match budget {
                    Some(budget) => match tokio::time::timeout(budget, render).await {
                        Ok(result) => Some(result),
                        Err(_) => {
                            warnings.push(format!(
                                "render_timeout_http_fallback: browser exceeded the learned {:.0} ms budget",
                                budget.as_millis()
                            ));
                            None
                        }
                    },
                    None => Some(render.await),
                };
                if let Some(outcome) = outcome {
                    match outcome {
                        Ok(rendered) => {
                            // A render that only reproduced the HTTP text is not
                            // an upgrade; keep the HTTP document and its lower
                            // memory footprint.
                            if text_weight(rendered.markdown.as_deref().unwrap_or_default())
                                > http_chars
                            {
                                return Ok(rendered);
                            }
                            warnings.push("render_no_gain_http_fallback".into());
                        }
                        Err(e) => {
                            // A failed upgrade is not a failed scrape: keep the
                            // HTTP result and say why the browser was not used.
                            warnings.push(format!("render_failed_http_fallback: {}", e.message));
                        }
                    }
                }
            } else {
                warnings.push(format!(
                    "render_unavailable: {}",
                    self.browser_unavailable_reason()
                ));
            }
        }

        let mut navigation = navigation;
        if opts.discover_scripts {
            // Release the document's admission permit before dependency fetches:
            // concurrent root pages must not occupy every per-host slot while
            // waiting on their own scripts.
            permit.take();
            let discovery_started = Instant::now();
            let work = crate::scripts::discover_script_graph(
                &self.policy,
                &html,
                &resp.final_url,
                Default::default(),
            );
            let graph = match request_deadline(opts, started) {
                Some(d) => tokio::time::timeout_at(d.into(), work).await.map_err(|_| {
                    ApiError::new(ErrorCode::Timeout, "script discovery deadline passed")
                })?,
                None => work.await,
            }
            .map_err(|e| ApiError::invalid(e.to_string()))?;
            if graph.incomplete {
                warnings.push("script_discovery_incomplete".into());
            }
            navigation.extend(
                graph
                    .edges
                    .into_iter()
                    .filter(|e| e.kind == "static-navigation")
                    .map(|e| e.url),
            );
            timings.insert("scriptDiscovery".into(), ms(discovery_started.elapsed()));
        }
        fill_metadata(&mut meta, &doc.metadata);
        fill_structured(&mut ext, &doc);
        fill_fingerprints(&mut ext, &doc);
        warnings.extend(doc.coverage.warnings());
        let raw_bytes = Some(thin_basis_bytes(&html, &doc.markdown));
        let video_fallback = doc.metadata.video_fallback;
        let mut result = document(doc, opts, html);
        if let Some(links) = result.links.as_mut() {
            for link in navigation {
                if !links.contains(&link) {
                    links.push(link);
                }
            }
        }
        Ok(finish(
            result,
            meta,
            ext,
            timings,
            warnings,
            raw_bytes,
            video_fallback,
            started,
        ))
    }
}

/// 不解析 DOM,只扫一遍字节:判断"值不值得为短页面再开一次浏览器"够用了。
fn has_script_tag(html: &str) -> bool {
    html.as_bytes()
        .windows(7)
        .any(|w| w.eq_ignore_ascii_case(b"<script"))
}

/// 页面本身几乎没有正文,只是用脚本跳到另一个地址。这样的页面可以给导航
/// 设一个按本页标签页获取耗时缩放的上限;真正的 JS 应用不适用。
fn has_client_redirect(html: &str) -> bool {
    const PATTERNS: &[&[u8]] = &[
        b"location.href",
        b"location.replace",
        b"location.assign",
        b"window.location",
    ];
    let bytes = html.as_bytes();
    PATTERNS
        .iter()
        .any(|p| bytes.windows(p.len()).any(|w| w.eq_ignore_ascii_case(p)))
}