These docs cover scrapegraph-py ≥ 2.1.0 and require Python ≥ 3.12. Earlier 1.x releases expose the deprecated v1 API and point to a different backend — none of the snippets on this page work there. The 2.0.x series used typed request wrappers (ScrapeRequest, ExtractRequest, …); 2.1.0 removed those wrappers in favour of direct positional/keyword arguments, so upgrade if you are pinned to 2.0.x.
schema= is a JSON Schema dict. Any Pydantic BaseModel produces one via model_json_schema(), so you can define the desired shape once and reuse it to validate the response client-side.
from pydantic import BaseModel, Fieldfrom scrapegraph_py import ScrapeGraphAIclass Product(BaseModel): name: str price: str | None = Noneclass Products(BaseModel): products: list[Product] = Field(default_factory=list)sgai = ScrapeGraphAI()res = sgai.extract( "Extract product names and prices", url="https://example.com", schema=Products.model_json_schema(),)if res.status == "success": parsed = Products.model_validate(res.data.json_data) for p in parsed.products: print(p.name, p.price)
The same pattern works for JsonFormatConfig(schema=...) in scrape() and for search(schema=...).
Run a web search and optionally extract structured data from the results.
from scrapegraph_py import ScrapeGraphAIsgai = ScrapeGraphAI()res = sgai.search( "best programming languages 2024", num_results=5, prompt="Summarize the top languages and reasons", time_range="past_week", location_geo_code="us",)if res.status == "success": for hit in res.data.results: print(hit.title, hit.url) print(res.data.json_data) # when prompt/schema are set
Non-empty MIME allowlist. Omit it for all supported types; "all" and "*" are not accepted
processors
list[PdfProcessor]
No
Omit for the 25-page PDF cap. PdfProcessor() also defaults to 25; set max_pages only to override it, or use -1 for unlimited. PDF processing costs 1 credit per page actually processed. Configuration examples
Non-empty MIME allowlist; omit it to allow every supported type
processors
list[PdfProcessor]
No
Omit for the 25-page PDF cap. PdfProcessor() also defaults to 25; set max_pages only to override it, or use -1 for unlimited. PDF processing costs 1 credit per page actually processed. Configuration examples
Paginate through the per-run ticks a monitor has produced (what changed on each scheduled run).
act = sgai.monitor.activity(cron_id, limit=20)if act.status == "success": for tick in act.data.ticks: status = "CHANGED" if tick.changed else "no change" print(f"[{tick.created_at}] {tick.status} - {status} ({tick.elapsed_ms}ms)") if act.data.next_cursor: more = sgai.monitor.activity(cron_id, limit=20, cursor=act.data.next_cursor)
monitor.activity() accepts limit (1–100, default 20) and optional cursor for pagination. Each MonitorTickEntry exposes id, created_at, status, changed, elapsed_ms, and a diffs model with per-format deltas.