Skip to content

URL Extraction

Fetch HTTP(S) URLs, extract their content, and optionally crawl from a seed URL — with a headless-browser fallback for JavaScript-rendered pages. URL ingestion reuses the same extract / extract_batch entry points as local files; a separate map_url function discovers the URL inventory of a site without extracting content.

  • You extract content directly from web pages or remote documents (a PDF behind a URL).
  • You crawl a site and extract every discovered page or downloaded document in one call.
  • You need a site’s URL inventory (sitemap / link graph) before committing to a full extraction run — use map_url.
  • You scrape JavaScript-rendered pages that need a real browser to produce content.
  • You only have local files or in-memory bytes. Use plain extract with a path or bytes input.
  • You need a full-featured standalone crawler with its own pipeline. Use crawlberg directly.
Function Returns Purpose
extract ExtractionResult Fetch an HTTP(S) URI and extract its content (single or crawl)
map_url MapResult Discover reachable URLs without extracting document content

An ExtractInput with kind = "uri" and an http/https URI routes through the URL-ingestion path. config.url (a UrlExtractionConfig) controls fetch and crawl behaviour.

UrlExtractionConfig.mode decides the shape of the run: Auto and Document fetch the single seed URL; Crawl follows links from the seed and extracts each discovered page or downloaded document.

extract_url.py
from xberg import CrawlConfig, ExtractInput, ExtractionConfig, UrlExtractionConfig, UrlExtractionMode, extract
config = ExtractionConfig(
url=UrlExtractionConfig(
mode=UrlExtractionMode.CRAWL,
crawl=CrawlConfig(max_depth=1, max_pages=25),
),
)
output = await extract(ExtractInput(kind="uri", uri="https://example.com"), config=config)
for result in output.results:
print(result.content)

extract_batch accepts a mix of URI and byte inputs, so remote pages and local files can be extracted in one call.

map_url builds a crawl engine from config.crawl and returns a MapResult — the set of discovered URLs — without extracting any document content. Use it to build a crawl queue or validate scope first.

map_url.py
from xberg import CrawlConfig, UrlExtractionConfig, map_url
result = await map_url(
"https://example.com",
UrlExtractionConfig(crawl=CrawlConfig(max_depth=2)),
)
for entry in result.urls:
print(entry.url)

MapResult.urls is a list of SitemapUrl entries, each carrying url plus optional lastmod, changefreq, and priority when the source sitemap provides them.

Static HTTP fetches miss content that only appears after JavaScript runs. CrawlConfig.browser (a BrowserConfig) controls when Xberg escalates to a headless browser. BrowserMode sets the policy:

Mode Behaviour
Auto Detect when JS rendering is needed and fall back to the browser (default).
Always Route every request through the browser tier.
Never Never use the browser fallback — static fetch only.
Stealth Like Always, plus JS stealth patches, TLS fingerprint spoofing, and viewport override.
browser_fallback.py
from xberg import (
BrowserConfig,
BrowserMode,
CrawlConfig,
ExtractInput,
ExtractionConfig,
UrlExtractionConfig,
extract,
)
config = ExtractionConfig(
url=UrlExtractionConfig(
crawl=CrawlConfig(
browser=BrowserConfig(mode=BrowserMode.ALWAYS, wait_selector="main"),
),
),
)
output = await extract(
ExtractInput(kind="uri", uri="https://spa.example.com"), config=config
)
print(output.results[0].content)

BrowserConfig also exposes backend (Chromiumoxide or Native), wait (NetworkIdle, Selector, Fixed), timeout, extra_wait, block_url_patterns, and eval_script. See the BrowserConfig reference.

UrlExtractionConfig controls ingestion scope and safety:

Field Type Default Purpose
mode UrlExtractionMode Auto Single-document fetch vs. crawl.
crawl CrawlConfig tuned defaults Crawl depth, concurrency, robots, browser, proxy, auth.
document_url_pattern str | None None Regex filter for document-discovered URLs.
max_document_urls_per_result int | None 100 Cap on URLs followed per extraction result.
max_total_urls int | None 1000 Cap on URLs followed across the whole call.
allow_local_file_inputs bool True Allow bare local filesystem path inputs.
allow_file_uris bool True Allow local file:// URI inputs.

CrawlConfig defaults are tuned for polite, bounded crawls: max_depth=1, max_pages=100, max_concurrent=10, respect_robots_txt=true, stay_on_domain=true. Override them for wider or narrower runs. See the full CrawlConfig reference.