HTTP client based on the `impit` library. This client uses the `impit` library to perform HTTP requests in crawlers (`BasicCrawler` subclasses) and to manage sessions, proxies, and error handling. See the `HttpClient` class for more common information about HTTP clients. ### Usage
| 71 | |
| 72 | @docs_group('HTTP clients') |
| 73 | class ImpitHttpClient(HttpClient): |
| 74 | """HTTP client based on the `impit` library. |
| 75 | |
| 76 | This client uses the `impit` library to perform HTTP requests in crawlers (`BasicCrawler` subclasses) |
| 77 | and to manage sessions, proxies, and error handling. |
| 78 | |
| 79 | See the `HttpClient` class for more common information about HTTP clients. |
| 80 | |
| 81 | ### Usage |
| 82 | |
| 83 | ```python |
| 84 | from crawlee.crawlers import HttpCrawler # or any other HTTP client-based crawler |
| 85 | from crawlee.http_clients import ImpitHttpClient |
| 86 | |
| 87 | http_client = ImpitHttpClient() |
| 88 | crawler = HttpCrawler(http_client=http_client) |
| 89 | ``` |
| 90 | """ |
| 91 | |
| 92 | def __init__( |
| 93 | self, |
| 94 | *, |
| 95 | persist_cookies_per_session: bool = True, |
| 96 | http3: bool = False, |
| 97 | verify: bool = True, |
| 98 | browser: Browser | None = 'firefox', |
| 99 | **async_client_kwargs: Any, |
| 100 | ) -> None: |
| 101 | """Initialize a new instance. |
| 102 | |
| 103 | Args: |
| 104 | persist_cookies_per_session: Whether to persist cookies per HTTP session. |
| 105 | http3: Whether to enable HTTP/3 support. |
| 106 | verify: SSL certificates used to verify the identity of requested hosts. |
| 107 | browser: Browser to impersonate. |
| 108 | async_client_kwargs: Additional keyword arguments for `impit.AsyncClient`. |
| 109 | """ |
| 110 | super().__init__( |
| 111 | persist_cookies_per_session=persist_cookies_per_session, |
| 112 | ) |
| 113 | self._http3 = http3 |
| 114 | self._verify = verify |
| 115 | self._browser = browser |
| 116 | |
| 117 | self._async_client_kwargs = async_client_kwargs |
| 118 | |
| 119 | self._client_by_proxy_url = LRUCache[str | None, _ClientCacheEntry](maxsize=10) |
| 120 | |
| 121 | @override |
| 122 | async def crawl( |
| 123 | self, |
| 124 | request: Request, |
| 125 | *, |
| 126 | session: Session | None = None, |
| 127 | proxy_info: ProxyInfo | None = None, |
| 128 | statistics: Statistics | None = None, |
| 129 | timeout: timedelta | None = None, |
| 130 | ) -> HttpCrawlingResult: |
no outgoing calls