|
| 1 | +--- |
| 2 | +title: "Extract API" |
| 3 | +description: "Extract page content, discover URLs, and crawl websites with the Python client." |
| 4 | +--- |
| 5 | + |
| 6 | +# Extract API |
| 7 | + |
| 8 | +Use `client.extract()` to read content from a public URL. It supports single-page scraping, URL discovery, and crawling. Pass a parameter dictionary or keyword arguments, as you would with `client.search()`. The client sets `engine="extract"` automatically. |
| 9 | + |
| 10 | +See the [Extract API documentation](https://serpapi.com/extract-api) for all parameters and response fields. |
| 11 | + |
| 12 | +## Get Only the Page Content as JSON |
| 13 | + |
| 14 | +Set `include_markdown=True` and `json_restrictor="text_content.markdown"` to get the page's Markdown content in a JSON response. This removes metadata and all other response sections. |
| 15 | + |
| 16 | +After [setting your API key](getting-started.md#installation), run: |
| 17 | + |
| 18 | +```python |
| 19 | +import os |
| 20 | +import serpapi |
| 21 | + |
| 22 | +client = serpapi.Client(api_key=os.environ["SERPAPI_KEY"], timeout=60) |
| 23 | +results = client.extract( |
| 24 | + url="https://example.com/", |
| 25 | + output="json", |
| 26 | + include_markdown=True, |
| 27 | + json_restrictor="text_content.markdown", |
| 28 | +) |
| 29 | + |
| 30 | +markdown = results["text_content"]["markdown"] |
| 31 | +print(markdown) |
| 32 | +``` |
| 33 | + |
| 34 | +The response has this structure, with the extracted page content in `markdown`: |
| 35 | + |
| 36 | +```json |
| 37 | +{ |
| 38 | + "text_content": { |
| 39 | + "markdown": "Page content in Markdown..." |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +The SDK returns a dictionary-like `SerpResults` object. Read `results["text_content"]["markdown"]` to get the plain Markdown string, as shown above. You can pass that string to an AI agent or save it to a file. |
| 45 | + |
| 46 | +See [JSON Restrictor](json-restrictor.md) for other ways to select response fields. |
| 47 | + |
| 48 | +## Get the Full Extract Response |
| 49 | + |
| 50 | +Leave out `json_restrictor` to receive the available extracted sections, such as page information, headings, links, and text content: |
| 51 | + |
| 52 | +```python |
| 53 | +results = client.extract( |
| 54 | + url="https://example.com/", |
| 55 | + include_markdown=True, |
| 56 | +) |
| 57 | + |
| 58 | +print(results["page_information"]["title"]) |
| 59 | +print(results["text_content"]["markdown"]) |
| 60 | +``` |
| 61 | + |
| 62 | +JSON is the default output format. `include_markdown=True` adds Markdown under `text_content`; the rest of the response remains JSON. The sections returned depend on the target page. |
| 63 | + |
| 64 | +The module-level `serpapi.extract(url=..., api_key=...)` function accepts the same parameters. |
| 65 | + |
| 66 | +## Markdown and HTML Responses |
| 67 | + |
| 68 | +Use `output="md"` or `output="html"` to receive a Python string: |
| 69 | + |
| 70 | +```python |
| 71 | +markdown_response = client.extract(url="https://example.com/", output="md") |
| 72 | +html = client.extract(url="https://example.com/", output="html") |
| 73 | +``` |
| 74 | + |
| 75 | +For a scraped page, `output="md"` includes search metadata before the page content. Use the [content-only JSON example](#get-only-the-page-content-as-json) when you want the page's Markdown without those extra sections. `output="html"` returns the retrieved HTML. |
| 76 | + |
| 77 | +## Discover URLs |
| 78 | + |
| 79 | +Set `mode="map"` to discover URLs on a website: |
| 80 | + |
| 81 | +```python |
| 82 | +results = client.extract( |
| 83 | + url="https://www.python.org/", |
| 84 | + mode="map", |
| 85 | + include_sitemap=False, |
| 86 | + include_subdomains=False, |
| 87 | +) |
| 88 | + |
| 89 | +for entry in results["urls"]: |
| 90 | + print(entry["url"]) |
| 91 | +``` |
| 92 | + |
| 93 | +Map mode includes sitemap URLs by default. Set `include_sitemap=False` to discover URLs from the page's links only. Set `include_subdomains=True` to include subdomains. |
| 94 | + |
| 95 | +## Crawl Multiple Pages |
| 96 | + |
| 97 | +Set `mode="crawl"` to follow links and extract pages: |
| 98 | + |
| 99 | +```python |
| 100 | +results = client.extract( |
| 101 | + url="https://www.python.org/", |
| 102 | + mode="crawl", |
| 103 | + max_pages=2, |
| 104 | + max_depth=1, |
| 105 | + timeout=180, |
| 106 | +) |
| 107 | + |
| 108 | +for page in results["pages"]: |
| 109 | + print(page["url"]) |
| 110 | +``` |
| 111 | + |
| 112 | +The starting page has depth `0`. `max_pages` includes the starting page and defaults to `10`. Each successfully extracted page can consume one search credit. Set a page limit for your crawl and allow enough time for the HTTP request to finish. For requests you want to retrieve later, pass `"async": True` in a parameter dictionary and use the [Search Archive](async-search-archive.md). |
| 113 | + |
| 114 | +## Timeouts and Content Filters |
| 115 | + |
| 116 | +The `timeout=` keyword sets the SDK's HTTP timeout in seconds. Set it above the API fetch timeout, and allow time for all pages when crawling. Put the Extract API's fetch timeout in the parameter dictionary to send it in milliseconds: |
| 117 | + |
| 118 | +```python |
| 119 | +results = client.extract( |
| 120 | + {"url": "https://example.com/", "timeout": 30000}, |
| 121 | + timeout=60, |
| 122 | +) |
| 123 | +``` |
| 124 | + |
| 125 | +Use `include_tags` and `exclude_tags` to keep or remove content with CSS selectors, such as `include_tags="article, main"` or `exclude_tags="nav, footer"`. See the [Extract API parameters](https://serpapi.com/extract-api) for details and [Errors and Timeouts](errors-and-timeouts.md) for exception handling. |
0 commit comments