Implementation:1broseidon Ketch scrape Scraper Scrape
| Knowledge Sources | |
|---|---|
| Domains | Scrape, HTTP_Fetch |
| Last Updated | 2026-10-27 00:00 GMT |
Overview
The core fetch: rewrites the URL, fetches via HTTP, routes PDFs to the PDF extractor, runs JS-shell detection with browser fallback, and extracts markdown.
Description
Scrape is the fast-path-first fetch. It applies the URL rewriter, calls `FetchContent` (HTTP GET with the 20 MiB body cap, cookies, UA), and routes `application/pdf` to the PDF extractor. For HTML it calls `MaybeBrowserFetch`, which runs the JS-shell detector and, on `"likely_shell"`, re-fetches via the headless browser. The extracted markdown is returned with a `source` tag (`SourceHTTP`, `SourceHTTPShell`, or `SourceBrowser`) that drives cache invalidation.
ScrapeConditional is the crawler's variant: it sends `ETag`/`If-Modified-Since` and returns a `FetchResult` carrying the parsed goquery doc (reused for link extraction) and the JS detection result. BrowserScrape skips HTTP entirely and goes straight to the browser (used when a host is known to need rendering).
Usage
`Scrape` is called by `CachedScrape` after a cache miss; callers should normally go through `CachedScrape` to get caching. `ScrapeConditional` is the crawler's per-page path. `BrowserScrape` is used by the crawler's adaptive browser escalation.
Code Reference
Source Location
- Repository: ketch
- File: scrape/scrape.go
Signature
const MaxBodyBytes = 20 << 20 // 20 MiB
const (
SourceHTTP = "http"
SourceHTTPShell = "http_shell"
SourceBrowser = "browser"
)
type Page struct {
URL string `json:"url"`
FetchedURL string `json:"fetched_url,omitempty"`
Title string `json:"title"`
Markdown string `json:"markdown"`
ETag string `json:"etag,omitempty"`
LastModified string `json:"last_modified,omitempty"`
ContentHash string `json:"content_hash,omitempty"`
}
// Scrape fetches a URL and returns extracted markdown + fetch source.
func (s *Scraper) Scrape(ctx context.Context, rawURL string) (*Page, string, error)
// ScrapeConditional fetches with conditional headers and JS detection.
func (s *Scraper) ScrapeConditional(ctx context.Context, rawURL, etag, lastModified string) (*FetchResult, error)
// BrowserScrape fetches via the browser directly (known JS-required host).
func (s *Scraper) BrowserScrape(ctx context.Context, rawURL string) (*Page, string, error)
// MaybeBrowserFetch re-fetches via the browser if html looks JS-rendered.
func (s *Scraper) MaybeBrowserFetch(ctx context.Context, rawURL, html string) (string, string)
Import
import "github.com/1broseidon/ketch/scrape"
I/O Contract
Inputs (Scrape)
| Name | Type | Required | Description |
|---|---|---|---|
| ctx | context.Context | Yes | Bounds the HTTP/browser fetch |
| rawURL | string | Yes | URL to fetch (rewritten internally) |
Outputs (Scrape)
| Name | Type | Description |
|---|---|---|
| page | *scrape.Page | Extracted markdown page |
| source | string | "http", "http_shell", or "browser" — drives cache invalidation |
| err | error | Fetch, PDF-extraction, or extraction failure |
Usage Examples
Direct Fetch (No Cache)
// Usually go through CachedScrape; Scrape is the no-cache fetch.
page, source, err := scraper.Scrape(ctx, "https://example.com")
if source == scrape.SourceHTTPShell {
fmt.Fprintln(os.Stderr, "warn: JS-rendered; configure a browser for full content")
}
Crawler Conditional Fetch
fr, err := scraper.ScrapeConditional(ctx, url, page.ETag, page.LastModified)
if fr.NotModified {
return // cached page still current
}
// fr.Doc is the parsed goquery doc — reuse it for link extraction.