Extract
See how to use extract() to extract structured data from web pages
Method Signatures
- TypeScript
- Python
// With schema and options
await page.extract<T extends z.AnyZodObject>(options: ExtractOptions<T>): Promise<ExtractResult<T>>
// String instruction only
await page.extract(instruction: string): Promise<{ extraction: string }>
// No parameters (raw page content)
await page.extract(): Promise<{ pageText: string }>
interface ExtractOptions<T extends z.AnyZodObject> {
instruction?: string;
schema?: T;
modelName?: AvailableModel;
modelClientOptions?: ClientOptions;
domSettleTimeoutMs?: number;
selector?: string;
iframes?: boolean;
}
type ExtractResult<T> = z.infer<T>;
# With schema and parameters
await page.extract(
instruction: str = None,
schema: BaseModel = None,
selector: str = None,
iframes: bool = None,
model_name: AvailableModel = None,
model_client_options: Dict = None,
dom_settle_timeout_ms: int = None
) -> ExtractResult
# String instruction only
await page.extract(instruction: str) -> Dict[str, str]
# No parameters (raw page content)
await page.extract() -> Dict[str, str]
Parameters
Natural language description of what data to extract.
Type schema defining the structure of data to extract. Ensures type safety and validation.
XPath selector to limit extraction scope. Reduces token usage and improves accuracy.
Set to
true if content exists within iframes.Default: falseOverride the default LLM model for this extraction.
Model-specific configuration options.
Maximum time to wait for DOM to stabilize.Default:
30000Response Types
- With Schema
- String Only
- No Parameters
Returns:
Promise<ExtractResult<T>> where T matches your schemaThe returned object will be strictly typed according to your schema definition.Returns:
Promise<{ extraction: string }>Simple string extraction without schema validation.Returns:
Promise<{ pageText: string }>Raw accessibility tree representation of page content.Code Examples
- Single Object
- Arrays
- URLs
- Scoped
- Schema-less
- Advanced
import { z } from 'zod';
// Schema definition
const ProductSchema = z.object({
name: z.string(),
price: z.number(),
inStock: z.boolean()
});
// Extraction
const product = await page.extract({
instruction: "extract product details",
schema: ProductSchema
});
from pydantic import BaseModel
# Schema definition
class Product(BaseModel):
name: str
price: float
in_stock: bool
# Extraction
product = await page.extract(
instruction="extract product details",
schema=Product
)
Example Response
{
"name": "Product Name",
"price": 100,
"inStock": true
}
import { z } from 'zod';
// Schema definition
const ApartmentListingsSchema = z.object({
apartments: z.array(z.object({
address: z.string(),
price: z.string(),
bedrooms: z.number()
}))
});
// Extraction
const listings = await page.extract({
instruction: "extract all apartment listings",
schema: ApartmentListingsSchema
});
from pydantic import BaseModel
from typing import List
# Schema definition
class Apartment(BaseModel):
address: str
price: str
bedrooms: int
class ApartmentListings(BaseModel):
apartments: List[Apartment]
# Extraction
listings = await page.extract(
instruction="extract all apartment listings",
schema=ApartmentListings
)
Example Response
{
"apartments": [
{
"address": "123 Main St",
"price": "$100,000",
"bedrooms": 3
},
{
"address": "456 Elm St",
"price": "$150,000",
"bedrooms": 2
}
]
}
import { z } from 'zod';
// Schema definition
const NavigationSchema = z.object({
links: z.array(z.object({
text: z.string(),
url: z.string().url() // URL validation
}))
});
// Extraction
const links = await page.extract({
instruction: "extract navigation links",
schema: NavigationSchema
});
from pydantic import BaseModel, HttpUrl
from typing import List
# Schema definition
class NavLink(BaseModel):
text: str
url: HttpUrl # URL validation
class Navigation(BaseModel):
links: List[NavLink]
# Extraction
links = await page.extract(
instruction="extract navigation links",
schema=Navigation
)
Example Response
{
"links": [
{
"text": "Home",
"url": "https://example.com"
}
]
}
import { z } from 'zod';
const ProductSchema = z.object({
name: z.string(),
price: z.number(),
description: z.string()
});
// Extract from specific page section
const data = await page.extract({
instruction: "extract product info from this section",
selector: "xpath=/html/body/div/div",
schema: ProductSchema
});
from pydantic import BaseModel
class Product(BaseModel):
name: str
price: float
description: str
# Extract from specific page section
data = await page.extract(
instruction="extract product info from this section",
selector="xpath=/html/body/div/div",
schema=Product
)
Example Response
{
"name": "Product Name",
"price": 100,
"description": "Product description"
}
// String only extraction
const title = await page.extract("get the page title");
// Returns: { extraction: "Page Title" }
// Raw page content
const content = await page.extract();
// Returns: { pageText: "Accessibility Tree: ..." }
# String only extraction
title = await page.extract("get the page title")
# Returns: {"extraction": "Page Title"}
# Raw page content
content = await page.extract()
# Returns: {"pageText": "Accessibility Tree: ..."}
Example Response
{
"extraction": "Page Title"
}
import { z } from 'zod';
// Schema with descriptions and validation
const ProductSchema = z.object({
price: z.number().describe("Product price in USD"),
rating: z.number().min(0).max(5).describe("Customer rating out of 5"),
available: z.boolean().describe("Whether product is in stock"),
tags: z.array(z.string()).optional()
});
// Nested schema
const EcommerceSchema = z.object({
product: z.object({
name: z.string(),
price: z.object({
current: z.number(),
original: z.number().optional()
})
}),
reviews: z.array(z.object({
rating: z.number(),
comment: z.string()
}))
});
from pydantic import BaseModel, Field
from typing import Optional, List
# Schema with descriptions and validation
class Product(BaseModel):
price: float = Field(description="Product price in USD")
rating: float = Field(ge=0, le=5, description="Customer rating out of 5")
available: bool = Field(description="Whether product is in stock")
tags: Optional[List[str]] = None
# Nested schema
class Price(BaseModel):
current: float
original: Optional[float] = None
class Review(BaseModel):
rating: int
comment: str
class ProductDetails(BaseModel):
name: str
price: Price
class EcommerceData(BaseModel):
product: ProductDetails
reviews: List[Review]
Example Response
{
"product": {
"name": "Product Name",
"price": {
"current": 100,
"original": 120
}
},
"reviews": [
{
"rating": 4,
"comment": "Great product!"
}
]
}

