Skip to main content

zyte-api-convertor

A Python module to convert Zyte API Json payload to Scrapy ZyteAPI project. It uses Scrapy and scrapy-zyte-api plugin to generate the project, also it uses black to format the code.

Requirements

Python 3.6+
Scrapy
scrapy-zyte-api
black

Documentation

Zyte API Documentation

Test the Zyte API payload using postman or curl. Once it gives the desired response, use the same payload with this module to convert it to a Scrapy ZyteAPI project.

Installation

pip install zyte-api-convertor

Usage

    Usage: zyte-api-convertor <payload> --project-name <project_name> --spider-name <spider_name>
    Example: zyte-api-convertor '{"url": "https://httpbin.org/ip", "browserHtml": true, "screenshot": true}' --project-name sample_project --spider-name sample_spider

    Usage: zyte-api-convertor <payload> --project-name <project_name>
    Example: zyte-api-convertor '{"url": "https://httpbin.org/ip", "browserHtml": true, "screenshot": true}' --project-name sample_project

    Usage: zyte-api-convertor <payload> --spider-name <spider_name>
    Example: zyte-api-convertor '{"url": "https://httpbin.org/ip", "browserHtml": true, "screenshot": true}' --spider-name sample_spider

    Usage: zyte-api-convertor <payload>
    Example: zyte-api-convertor '{"url": "https://httpbin.org/ip", "browserHtml": true, "screenshot": true}'

Example

zyte-api-convertor expects a valid json payload at the least. But it does have other options as well. You can use the --project-name and --spider-name options to set the project and spider name. If you don't use these options, it will use the default project and spider name.

zyte-api-convertor '{"url": "https://httpbin.org/ip", "browserHtml": true, "screenshot": true}' --project-name sample_project --spider-name sample_spider

Output:

mukthy@Mukthys-MacBook-Pro % zyte-api-convertor '{"url": "https://httpbin.org/ip", "browserHtml": true, "screenshot": true}' --project-name sample_project --spider-name sample_spider
Code Generated!
Writing to file...
Writing Done!
reformatted sample_project/sample_project/spiders/sample_project.py

All done!  🍰 1 file reformatted.
Formatting Done!

Project Created Successfully.

mukthy@Mukthys-MacBook-Pro %  sample_project % tree
.
├── sample_project
│   ├── __init__.py
│   ├── items.py
│   ├── middlewares.py
│   ├── pipelines.py
│   ├── settings.py
│   └── spiders
│       ├── __init__.py
│       └── sample_project.py
└── scrapy.cfg

3 directories, 8 files

Sample Spider Code:

import scrapy


class SampleQuotesSpider(scrapy.Spider):
    name = "sample_spider"

    custom_settings = {
        "DOWNLOAD_HANDLERS": {
            "http": "scrapy_zyte_api.ScrapyZyteAPIDownloadHandler",
            "https": "scrapy_zyte_api.ScrapyZyteAPIDownloadHandler",
        },
        "DOWNLOADER_MIDDLEWARES": {
            "scrapy_zyte_api.ScrapyZyteAPIDownloaderMiddleware": 1000
        },
        "REQUEST_FINGERPRINTER_CLASS": "scrapy_zyte_api.ScrapyZyteAPIRequestFingerprinter",
        "TWISTED_REACTOR": "twisted.internet.asyncioreactor.AsyncioSelectorReactor",
        "ZYTE_API_KEY": "YOUR_API_KEY",
    }

    def start_requests(self):
        yield scrapy.Request(
            url="https://httpbin.org/ip",
            meta={
                "zyte_api": {
                    "javascript": False,
                    "screenshot": True,
                    "browserHtml": True,
                    "actions": [],
                    "requestHeaders": {},
                    "geolocation": "US",
                    "experimental": {"responseCookies": False},
                }
            },
        )

    def parse(self, response):
        print(response.text)

Please note that the ZYTE_API_KEY is not set in the custom_settings of the spider. You need to set it before running it.

Release files for zyte-api-convertor 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for zyte-api-convertor 1.0.3
File Size Uploaded
zyte_api_convertor-1.0.3.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for zyte-api-convertor 1.0.3
File Interpreter ABI Platform
zyte_api_convertor-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 12.1 kB

Release files / zyte_api_convertor-1.0.3.tar.gz

Download URL zyte_api_convertor-1.0.3.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
ee464e45c21f8e7ebebc7f90a12f80c5062fe3054808401199eccaf4df30146e
BLAKE2b-256 checksum
How to use checksums
5ddb04402002c8f0786b93bdaed3ef4543d740b652818bb72ce8b3802339b016
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.15

Release files / zyte_api_convertor-1.0.3-py3-none-any.whl

Download URL zyte_api_convertor-1.0.3-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6b1fb357ca7f52d4705e4578768981d20286ba3d34d2c8481514ad74379fe44e
BLAKE2b-256 checksum
How to use checksums
df24fa661dc8fbc3684611013da1020204754fd07be47a15b0a160a6cf3598ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.15

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page