Zyte API Python Client

0.9.0 · active · verified Thu Apr 16

The `zyte-api` Python client provides an asynchronous interface to the Zyte API, enabling programmatic access to web scraping functionalities such as smart browser rendering, automatic content extraction, and HTTP requests. It handles API communication, retries, and result parsing. Currently at version 0.9.0, it sees active development with frequent minor releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `ZyteAPI` client with an API key (preferably from an environment variable) and make an asynchronous `request_render` call to fetch HTML, a screenshot, and page data for a given URL. It uses `asyncio.run()` to execute the asynchronous operation.

import os
import asyncio
from zyte_api.zyte_api import ZyteAPI

api_key = os.environ.get('ZYTE_API_KEY', '') # Set ZYTE_API_KEY environment variable or replace with your key

async def main():
    if not api_key:
        print("ZYTE_API_KEY environment variable not set. Skipping quickstart example.")
        return

    api = ZyteAPI(api_key=api_key)
    try:
        print("Making a request to Zyte API for www.zyte.com...")
        result = await api.request_render(
            url="https://www.zyte.com/",
            browserHtml=True,
            screenshot=True,
            httpResponseBody=True,
            javascript=True
        )

        print(f"Request Status: {result.status}")
        if result.browserData:
            print(f"Page title: {result.browserData.title}")
        if result.browserHtml:
            print(f"HTML (first 100 chars): {result.browserHtml[:100]}...")
        if result.screenshot:
            print(f"Screenshot (base64, first 50 chars): {result.screenshot[:50]}...")
        print("Successfully received Zyte API response.")

    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == '__main__':
    asyncio.run(main())

view raw JSON →