Enable Zyte API to avoid bans#

Your target domain in this tutorial, toscrape.com, does not ban traffic. However, when targeting other websites, sooner or later you will get bans.

You will now configure your web scraping code to use Zyte API to avoid bans on any website:

  1. Sign up for Zyte API. You get $5 free for a month, and you should only need a fraction of that to complete this tutorial.

  2. Install the latest version of scrapy-zyte-api:

    pip install --upgrade scrapy-zyte-api
    
  3. Configure scrapy-zyte-api in transparent mode by adding the following code at the end of tutorial/settings.py, replacing YOUR_API_KEY with your API key:

    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"
    ZYTE_API_KEY = "YOUR_API_KEY"
    ZYTE_API_TRANSPARENT_MODE = True
    

If you run your code again:

scrapy crawl books_toscrape_com -O books.csv

Your code will work the same, only that requests will be sent through Zyte API, to avoid bans cost-efficiently.

Tip

If you get an SSL error, install the Zyte CA certificate on your system and try again.