Skip to main content

Scrapy middleware for sending requests with aiohttp.

Project description

scrapy-aiohttp

version pyversions Tests codecov

This library simply integrates aiohttp with Scrapy, addressing challenges encountered when dealing with websites imposing restrictions on standard Scrapy requests. Specifically, it simply resolves the 403 Forbidden issue often encountered when utilizing Scrapy's built-in requests.

Installation

To install scrapy-aiohttp, use the following command:

$ pip install scrapy-aiohttp

Configuration

  1. Add the aiohttp server address to settings.py of your Scrapy project like this:
AIOHTTP_SERVER_URL = "http://localhost:8080/"
  1. Enable the scrapy-aiohttp middleware by adding it to DOWNLOADER_MIDDLEWARES in your settings.py file:
DOWNLOAD_HANDLERS = {
    "scrapy_aiohttp.AiohttpMiddleware": 651,
}
  1. Set up aiohttp request headers configuration to settings.py like this:
from scrapy_aiohttp.utils import DEFAULT_AIOHTTP_REQUEST_HEADERS_CONFIG

AIOHTTP_REQUEST_HEADERS_CONFIG = DEFAULT_AIOHTTP_REQUEST_HEADERS_CONFIG

Type dict[str, str | Callable[[aiohttp.web.Request], str] | None]

The AIOHTTP_REQUEST_HEADERS_CONFIG serves as an interface for inheriting headers from a Scrapy request and reusing them to create an aiohttp request.

DEFAULT_AIOHTTP_REQUEST_HEADERS_CONFIG is initialized with the values given below in the example.

Here's how you can customize AIOHTTP_REQUEST_HEADERS_CONFIG using the following guidelines:

  • If the header value is a Callable object (a function), it gets executed with the HTTP request object (aiohttp.web.Request) as an argument during header construction. Result of the executed function becomes the header value.
    {"Host": lambda request: urlparse(request.match_info.get("url")).hostname}
    
  • If the header value is a str, it serves as a static value for the header.
    {"Content-Type": "text/html"}
    
  • If the header value is set to None, it implies that the header should be inherited from the request headers. In other words, the server will use the same value for this header as it receives in the incoming request.
     {"User-Agent": None}
    

Note: Headers missing in AIOHTTP_REQUEST_HEADERS_CONFIG will not be applied to the aiohttp request!

Ensure that all necessary headers are defined to meet your specific requirements.

Usage

The easiest way to send requests with aiohttp is to use scrapy_aiohttp.AiohttpRequest. You can also use regular scrapy.Request and the "aiohttp" Request meta key:

from scrapy import Spider, Request
from scrapy_aiohttp import AiohttpRequest


class ExampleSpider(Spider):
    name = "example"

    def start_requests(self):
        # use case: scrapy_aiohttp.AiohttpRequest
        yield AiohttpRequest(
            url="https://example.com",
            callback=self.parse
        )
        # use case: scrapy.Request with meta key
        yield Request(
            url="https://example.com",
            callback=self.parse,
            meta={"aiohttp": True},
        )

    def parse(self, response, **kwargs):
        return {"url": response.url}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scrapy_aiohttp-0.1.2.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

scrapy_aiohttp-0.1.2-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file scrapy_aiohttp-0.1.2.tar.gz.

File metadata

  • Download URL: scrapy_aiohttp-0.1.2.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for scrapy_aiohttp-0.1.2.tar.gz
Algorithm Hash digest
SHA256 b9a4b50beaabc82e63a2e87b88dc43a66e2c4cd83fcda551e15798458163f25a
MD5 535c254a3139aeb19ed8388bc7c5dcf5
BLAKE2b-256 5ea4d93ca306139a7a2636a4797c42d8e56907a96a00229b4fe86f78c0f46366

See more details on using hashes here.

File details

Details for the file scrapy_aiohttp-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: scrapy_aiohttp-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for scrapy_aiohttp-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 043c64d59a720d14c5fc32f2edfa56cf859751d93d9f61075c298360739f4c42
MD5 449f50eeca0faa9632b4bdae272b6dd5
BLAKE2b-256 b6cd2fe0ea07bbb779e80c9f81ffba9f1fec04f43bb3f32391d3ec37270c2287

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page