Run crawls¶
run crawls one config and run_many several, with the same
RunOptions that databrew run builds from its flags.
parse shows what one page extracts without a crawl. The report they return
holds a stop and an outcome.
databrew.run
¶
run(
config: str | Path | WebsiteConfig,
*,
options: RunOptions | None = None,
middleware: Sequence[Middleware] = (),
fetcher: Fetcher | None = None,
) -> RunReport
Run one crawl and return its report.
config is a config file or a loaded config. middleware runs after the
[[middleware]] entries. run closes the entries it builds, and the caller closes
middleware. fetcher replaces the one [fetch] describes, and run closes it when
it ends. On the main thread, the first SIGINT or SIGTERM stops the crawl: requests in flight
finish, unfinished URLs return to the queue, and the report records the interrupted stop.
A caller with its own handler for that signal, or that ignores it, keeps it, and the signal
gets its previous handler back when run ends. A loaded config whose setup
raises, or whose store cannot write its items when it closes, gets a report with the error
stop, which is also its .last_run.json.
Raises:
| Type | Description |
|---|---|
ConfigError
|
The config cannot be loaded. |
BlockingIOError
|
Another process holds the config's store. Nothing is written to it. |
TypeError
|
|
Source code in src/databrew/runner.py
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 | |
databrew.run_many
¶
run_many(
configs: Sequence[str | Path | WebsiteConfig],
*,
parallel: int = 1,
deadline: float | None = None,
options: RunOptions | None = None,
) -> list[RunReport]
Run configs, up to parallel at once, and return their reports in order.
The first SIGINT or SIGTERM, or deadline seconds after the call, stops every running
crawl as run stops on a signal, and no config starts after it. A config that never
started gets a failed report and writes nothing to its store. Signals follow the rule of
run. A config that cannot load, or whose run raises, gets a failed report with the
error stop, and the others run. A handler with RunNameFilter labels each log record
with the name of the config that logged it. With more than one config, databrew.progress
logs each config's result at INFO when that config ends. Raises ValueError when parallel
is below 1 or deadline is not a number.
Source code in src/databrew/runner.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | |
databrew.RunOptions
¶
RunOptions(
max_requests: int | None = None,
concurrency: int | None = None,
rescan: bool = False,
retry_failed: bool = False,
passes: int | None = 1,
extra_start_urls: tuple[str | HttpUrl, ...] = (),
)
Settings for one invocation, as the databrew run flags express them.
A value out of range, or a pair of options that exclude each other, raises ValueError.
max_requests
¶
max_requests: int | None = None
Fetch attempts the run may make, retries included, in place of [policy]'s.
rescan
¶
rescan: bool = False
Begin an exhaustive traversal in place of the active one, keeping item URLs and their
failures. Excludes retry_failed.
retry_failed
¶
retry_failed: bool = False
Fetch only URLs with a failure on record, in one pass, and queue what they link to for
the next run. Excludes rescan, extra_start_urls, and passes other than 1.
passes
¶
passes: int | None = 1
Passes over the failed URLs, and what recovered pages link to, once the queue empties;
None adds passes until one recovers no URL.
extra_start_urls
¶
extra_start_urls: tuple[str | HttpUrl, ...] = ()
Start URLs after the config's own, each an HttpUrl. They join the traversal this run
begins, and every run must name them until that traversal completes.
apply
¶
apply(config: WebsiteConfig) -> WebsiteConfig
Return a copy of config with these options in place.
Source code in src/databrew/runner.py
107 108 109 110 111 112 113 114 115 116 | |
databrew.parse
¶
parse(
config: str | Path | WebsiteConfig,
url: str,
*,
page_type: Literal["listing", "item"] = "listing",
text: str | None = None,
save: str | Path | None = None,
) -> dict[str, Any]
Return what a run would extract from one page, writing no crawl state.
The page takes one fetch, with no retry, through the middleware's pre_fetch,
post_fetch, and post_extract; on_error and on_challenge do not run. With
text it starts there, without a fetch or pre_fetch. save keeps the fetched body in
that file. The result holds url, page_type, status, items,
pagination_links, item_links, diagnostics, and error, the message of a failure
or None.
Raises:
| Type | Description |
|---|---|
ConfigError
|
The config cannot be loaded or built, or names no |
ValueError
|
|
Source code in src/databrew/runner.py
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 | |