Module scrapfly.client

ScrapflyClient: the Scrapfly API over requests.

Functions

def read_stream(streamed: Streamed,
run: Callable[[Steps], Any] | None) ‑> Iterator
Expand source code
def read_stream(streamed: Streamed, run: Optional[Callable[[Steps], Any]]) -> Iterator:
    """The items of a streamed requests.Response, each through its finishing operation."""
    try:
        for chunk in streamed.response.iter_content(chunk_size=streamed.chunk_size):
            for item in streamed.decoder.feed(chunk):
                yield item if streamed.finish is None else run(streamed.finish(item))

            if streamed.decoder.done:
                return

        for item in streamed.decoder.close():
            yield item if streamed.finish is None else run(streamed.finish(item))
    finally:
        streamed.response.close()

The items of a streamed requests.Response, each through its finishing operation.

Classes

class ScrapflyClient (key: str,
host: str = 'https://api.scrapfly.io',
verify=True,
debug: bool = False,
max_concurrency: int = 1,
connect_timeout: int = 30,
web_scraping_api_read_timeout: int = 160,
extraction_api_read_timeout: int = 165,
screenshot_api_read_timeout: int = 130,
read_timeout: int = 30,
default_read_timeout: int = 30,
reporter: Callable | None = None,
cloud_browser_host: str | None = None,
**kwargs)
Expand source code
class ScrapflyClient(ScrapflyApi):
    """The Scrapfly API over requests. Every operation of ScrapflyApi is a plain method here."""

    def _init_transport(self):
        self.async_executor = ThreadPoolExecutor()
        self.http_session = None

        if self.debug is True:
            http.client.HTTPConnection.debuglevel = 5

    @cached_property
    def _http_handler(self):
        return partial(self.http_session.request if self.http_session else requests.request)

    @property
    def http(self):
        return self._http_handler

    @staticmethod
    def _method(steps):
        @wraps(steps)
        def method(self, *args, **kwargs):
            result = self._run(steps(self, *args, **kwargs))

            return read_stream(result, self._run) if isinstance(result, Streamed) else result

        return signed(method, steps, Iterator)

    @staticmethod
    def _stream_method(steps):
        def items(self, args, kwargs):
            yield from read_stream(self._run(steps(self, *args, **kwargs)), self._run)

        # A plain function returning a lazy iterator: nothing is sent before the first item is asked for.
        @wraps(steps)
        def method(self, *args, **kwargs):
            return items(self, args, kwargs)

        return signed(method, steps, Iterator)

    def _run(self, steps: Steps):
        send, value = steps.send, None

        while True:
            try:
                step = send(value)
            except StopIteration as done:
                return done.value
            except requests.RequestException as error:
                # raise_for_status() prints the request URL, key included.
                masked = without_key(error)
                if masked is error:
                    raise

                raise masked from None

            # Raised inside the operation, at the yield, so its own handlers see it.
            try:
                value, send = self._perform(step), steps.send
            except BaseException as error:
                value, send = error, steps.throw

    def _perform(self, step: Call | Sleep) -> Optional[Response]:
        if isinstance(step, Sleep):
            time.sleep(step.seconds)

            return None

        for attempt in range(step.tries):
            try:
                return self._http_handler(method=step.method, url=step.url, **step.options)
            except (*NetworkError, requests.RequestException) as error:
                retries = isinstance(error, NetworkError) and attempt + 1 < step.tries

                if _is_read_timeout(error) and not step.retry_read_timeout:
                    retries = False

                if retries:
                    time.sleep(retry_delay(attempt))
                    continue

                masked = without_key(error)
                if masked is error:
                    raise

                # The chained urllib3 errors print the key too.
                raise masked from None

    # The private methods of the client before ScrapflyApi, still called by the Scrapy downloader and subclasses.

    def _handle_response(self, response: Response, scrape_config: ScrapeConfig) -> ScrapeApiResponse:
        return self._run(self._scrape_response_steps(response, scrape_config))

    def _handle_api_response(self, response: Response, scrape_config: ScrapeConfig, raise_on_upstream_error: Optional[bool] = True) -> ScrapeApiResponse:
        return self._build_scrape_api_response(response, scrape_config, self._scrape_body(response, scrape_config), self._handle_scrape_large_objects, raise_on_upstream_error)

    def _handle_scrape_large_objects(self, callback_url: str, format: Literal['clob', 'blob']) -> Tuple[Union[BytesIO, str], str]:
        return self._run(self._large_object_steps(callback_url, format))

    def _monitoring_request(self, path: str, params: dict):
        return self._run(self._monitoring_steps(path, params))

    def _schedule_request(self, method: str, path: str, *, query: Optional[Dict[str, str]] = None, body: Optional[Dict[str, Any]] = None) -> Any:
        return self._run(self._schedule_steps(method, path, query=query, body=body))

    def _create_schedule(self, path: str, config_key: str, config: Dict[str, Any], request: CreateScheduleRequest) -> Schedule:
        return self._run(self._create_schedule_steps(path, config_key, config, request))

    @staticmethod
    def _iter_prompt_events(response: Response) -> Iterator[CrawlerPromptEvent]:
        yield from read_stream(Streamed(response=response, decoder=PromptEventDecoder(response.status_code), chunk_size=512), None)

    def open(self):
        if self.http_session is None:
            self.http_session = Session()
            self.http_session.verify = self.verify
            self.http_session.timeout = (self.connect_timeout, self.default_read_timeout)
            self.http_session.params['key'] = self.key
            self.http_session.headers['accept-encoding'] = self.body_handler.content_encoding
            self.http_session.headers['accept'] = self.body_handler.accept
            self.http_session.headers['user-agent'] = self.ua

    def close(self):
        if self.http_session is not None:
            self.http_session.close()
            self.http_session = None
        # The executor owns worker threads that outlive the HTTP session.
        if self.async_executor is not None:
            self.async_executor.shutdown(wait=False)
            self.async_executor = None

    def __enter__(self) -> 'ScrapflyClient':
        self.open()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()

    # Thread-pool helpers kept from before AsyncScrapflyClient, which awaits the same calls natively.

    async def async_scrape(self, scrape_config:ScrapeConfig, loop:Optional[AbstractEventLoop]=None) -> ScrapeApiResponse:
        if loop is None:
            loop = asyncio.get_running_loop()

        return await loop.run_in_executor(self.async_executor, self.scrape, scrape_config)

    async def async_screenshot(self, screenshot_config:ScreenshotConfig, loop:Optional[AbstractEventLoop]=None) -> ScreenshotApiResponse:
        if loop is None:
            loop = asyncio.get_running_loop()

        return await loop.run_in_executor(self.async_executor, self.screenshot, screenshot_config)

    async def async_extraction(self, extraction_config:ExtractionConfig, loop:Optional[AbstractEventLoop]=None) -> ExtractionApiResponse:
        if loop is None:
            loop = asyncio.get_running_loop()

        return await loop.run_in_executor(self.async_executor, self.extract, extraction_config)

    async def concurrent_scrape(self, scrape_configs:List[ScrapeConfig], concurrency:Optional[int]=None):
        if concurrency is None:
            concurrency = self.max_concurrency
        elif concurrency == self.CONCURRENCY_AUTO:
            concurrency = self.account()['subscription']['max_concurrency']

        loop = asyncio.get_running_loop()
        processing_tasks = []
        results = []
        processed_tasks = 0
        expected_tasks = len(scrape_configs)

        def scrape_done_callback(task:Task):
            nonlocal processed_tasks

            try:
                if task.cancelled() is True:
                    return

                error = task.exception()

                if error is not None:
                    results.append(error)
                else:
                    results.append(task.result())
            finally:
                processing_tasks.remove(task)
                processed_tasks += 1

        while scrape_configs or results or processing_tasks:
            logger.info("Scrape %d/%d - %d running" % (processed_tasks, expected_tasks, len(processing_tasks)))

            if scrape_configs:
                if len(processing_tasks) < concurrency:
                    # @todo handle backpressure
                    for _ in range(0, concurrency - len(processing_tasks)):
                        try:
                            scrape_config = scrape_configs.pop()
                        except IndexError:
                            break

                        scrape_config.raise_on_upstream_error = False
                        task = loop.create_task(self.async_scrape(scrape_config=scrape_config, loop=loop))
                        processing_tasks.append(task)
                        task.add_done_callback(scrape_done_callback)

            for _ in results:
                result = results.pop()
                yield result

            await asyncio.sleep(.5)

        logger.debug("Scrape %d/%d - %d running" % (processed_tasks, expected_tasks, len(processing_tasks)))

The Scrapfly API over requests. Every operation of ScrapflyApi is a plain method here.

Ancestors

Instance variables

prop http
Expand source code
@property
def http(self):
    return self._http_handler

Methods

def account(self) ‑> Steps[str | Dict]
Expand source code
@operation
def account(self) -> Steps[Union[str, Dict]]:
    response = yield Call(
        method='GET',
        url=self.host + '/account',
        params={'key': self.key},
        verify=self.verify,
        headers={
            'accept-encoding': self.body_handler.content_encoding,
            'accept': self.body_handler.accept,
            'user-agent': self.ua
        },
    )

    response.raise_for_status()

    if self.body_handler.support(response.headers):
        return self.body_handler(response.content, response.headers['content-type'])

    return response.content.decode('utf-8')
async def async_extraction(self,
extraction_config: ExtractionConfig,
loop: asyncio.events.AbstractEventLoop | None = None) ‑> ExtractionApiResponse
Expand source code
async def async_extraction(self, extraction_config:ExtractionConfig, loop:Optional[AbstractEventLoop]=None) -> ExtractionApiResponse:
    if loop is None:
        loop = asyncio.get_running_loop()

    return await loop.run_in_executor(self.async_executor, self.extract, extraction_config)
async def async_scrape(self,
scrape_config: ScrapeConfig,
loop: asyncio.events.AbstractEventLoop | None = None) ‑> ScrapeApiResponse
Expand source code
async def async_scrape(self, scrape_config:ScrapeConfig, loop:Optional[AbstractEventLoop]=None) -> ScrapeApiResponse:
    if loop is None:
        loop = asyncio.get_running_loop()

    return await loop.run_in_executor(self.async_executor, self.scrape, scrape_config)
async def async_screenshot(self,
screenshot_config: ScreenshotConfig,
loop: asyncio.events.AbstractEventLoop | None = None) ‑> ScreenshotApiResponse
Expand source code
async def async_screenshot(self, screenshot_config:ScreenshotConfig, loop:Optional[AbstractEventLoop]=None) -> ScreenshotApiResponse:
    if loop is None:
        loop = asyncio.get_running_loop()

    return await loop.run_in_executor(self.async_executor, self.screenshot, screenshot_config)
def close(self)
Expand source code
def close(self):
    if self.http_session is not None:
        self.http_session.close()
        self.http_session = None
    # The executor owns worker threads that outlive the HTTP session.
    if self.async_executor is not None:
        self.async_executor.shutdown(wait=False)
        self.async_executor = None
async def concurrent_scrape(self,
scrape_configs: List[ScrapeConfig],
concurrency: int | None = None)
Expand source code
async def concurrent_scrape(self, scrape_configs:List[ScrapeConfig], concurrency:Optional[int]=None):
    if concurrency is None:
        concurrency = self.max_concurrency
    elif concurrency == self.CONCURRENCY_AUTO:
        concurrency = self.account()['subscription']['max_concurrency']

    loop = asyncio.get_running_loop()
    processing_tasks = []
    results = []
    processed_tasks = 0
    expected_tasks = len(scrape_configs)

    def scrape_done_callback(task:Task):
        nonlocal processed_tasks

        try:
            if task.cancelled() is True:
                return

            error = task.exception()

            if error is not None:
                results.append(error)
            else:
                results.append(task.result())
        finally:
            processing_tasks.remove(task)
            processed_tasks += 1

    while scrape_configs or results or processing_tasks:
        logger.info("Scrape %d/%d - %d running" % (processed_tasks, expected_tasks, len(processing_tasks)))

        if scrape_configs:
            if len(processing_tasks) < concurrency:
                # @todo handle backpressure
                for _ in range(0, concurrency - len(processing_tasks)):
                    try:
                        scrape_config = scrape_configs.pop()
                    except IndexError:
                        break

                    scrape_config.raise_on_upstream_error = False
                    task = loop.create_task(self.async_scrape(scrape_config=scrape_config, loop=loop))
                    processing_tasks.append(task)
                    task.add_done_callback(scrape_done_callback)

        for _ in results:
            result = results.pop()
            yield result

        await asyncio.sleep(.5)

    logger.debug("Scrape %d/%d - %d running" % (processed_tasks, expected_tasks, len(processing_tasks)))
def get_browser_monitoring_metrics(self,
period: str | None = None,
proxy_pool: str | None = None,
start: datetime.datetime | None = None,
end: datetime.datetime | None = None)
Expand source code
@operation
def get_browser_monitoring_metrics(
    self,
    period:Optional[str]=None,
    proxy_pool:Optional[str]=None,
    start:Optional[datetime.datetime]=None,
    end:Optional[datetime.datetime]=None,
):
    if (start is not None and end is None) or (start is None and end is not None):
        raise ValueError('You must provide both start and end date')
    params:dict = {'key': self.key}
    if start is not None and end is not None:
        params['start'] = self._format_monitoring_dt(start)
        params['end'] = self._format_monitoring_dt(end)
    elif period is not None:
        params['period'] = period
    if proxy_pool is not None:
        params['proxy_pool'] = proxy_pool
    return (yield from self._monitoring_steps('/browser/monitoring/metrics', params))
def get_browser_monitoring_timeseries(self,
period: str | None = None,
proxy_pool: str | None = None,
start: datetime.datetime | None = None,
end: datetime.datetime | None = None)
Expand source code
@operation
def get_browser_monitoring_timeseries(
    self,
    period:Optional[str]=None,
    proxy_pool:Optional[str]=None,
    start:Optional[datetime.datetime]=None,
    end:Optional[datetime.datetime]=None,
):
    if (start is not None and end is None) or (start is None and end is not None):
        raise ValueError('You must provide both start and end date')
    params:dict = {'key': self.key}
    if start is not None and end is not None:
        params['start'] = self._format_monitoring_dt(start)
        params['end'] = self._format_monitoring_dt(end)
    elif period is not None:
        params['period'] = period
    if proxy_pool is not None:
        params['proxy_pool'] = proxy_pool
    return (yield from self._monitoring_steps('/browser/monitoring/metrics/timeseries', params))
def get_crawler_monitoring_metrics(self,
format: str = 'structured',
period: str | None = None,
aggregation: List[Literal['account', 'project', 'target']] | None = None,
include_webhook: bool = False)
Expand source code
@operation
def get_crawler_monitoring_metrics(
    self,
    format:str=ScraperAPI.MONITORING_DATA_FORMAT_STRUCTURED,
    period:Optional[str]=None,
    aggregation:Optional[List[MonitoringAggregation]]=None,
    include_webhook:bool=False,
):
    return (yield from self._monitoring_steps(
        '/crawl/monitoring/metrics',
        self._build_metrics_params(format, period, aggregation, include_webhook),
    ))
def get_crawler_monitoring_target_metrics(self,
domain: str,
group_subdomain: bool = False,
period: Literal['subscription', 'last7d', 'last24h', 'last1h', 'last5m'] | None = 'last24h',
start: datetime.datetime | None = None,
end: datetime.datetime | None = None,
include_webhook: bool = False)
Expand source code
@operation
def get_crawler_monitoring_target_metrics(
    self,
    domain:str,
    group_subdomain:bool=False,
    period:Optional[MonitoringTargetPeriod]=ScraperAPI.MONITORING_PERIOD_LAST_24H,
    start:Optional[datetime.datetime]=None,
    end:Optional[datetime.datetime]=None,
    include_webhook:bool=False,
):
    return (yield from self._monitoring_steps(
        '/crawl/monitoring/metrics/target',
        self._build_target_params(domain, group_subdomain, period, start, end, include_webhook),
    ))
def get_extraction_monitoring_metrics(self,
format: str = 'structured',
period: str | None = None,
aggregation: List[Literal['account', 'project', 'target']] | None = None,
include_webhook: bool = False)
Expand source code
@operation
def get_extraction_monitoring_metrics(
    self,
    format:str=ScraperAPI.MONITORING_DATA_FORMAT_STRUCTURED,
    period:Optional[str]=None,
    aggregation:Optional[List[MonitoringAggregation]]=None,
    include_webhook:bool=False,
):
    return (yield from self._monitoring_steps(
        '/extraction/monitoring/metrics',
        self._build_metrics_params(format, period, aggregation, include_webhook),
    ))
def get_extraction_monitoring_target_metrics(self,
domain: str,
group_subdomain: bool = False,
period: Literal['subscription', 'last7d', 'last24h', 'last1h', 'last5m'] | None = 'last24h',
start: datetime.datetime | None = None,
end: datetime.datetime | None = None,
include_webhook: bool = False)
Expand source code
@operation
def get_extraction_monitoring_target_metrics(
    self,
    domain:str,
    group_subdomain:bool=False,
    period:Optional[MonitoringTargetPeriod]=ScraperAPI.MONITORING_PERIOD_LAST_24H,
    start:Optional[datetime.datetime]=None,
    end:Optional[datetime.datetime]=None,
    include_webhook:bool=False,
):
    return (yield from self._monitoring_steps(
        '/extraction/monitoring/metrics/target',
        self._build_target_params(domain, group_subdomain, period, start, end, include_webhook),
    ))
def get_monitoring_metrics(self,
format: str = 'structured',
period: str | None = None,
aggregation: List[Literal['account', 'project', 'target']] | None = None,
include_webhook: bool = False)
Expand source code
@operation
def get_monitoring_metrics(
    self,
    format:str=ScraperAPI.MONITORING_DATA_FORMAT_STRUCTURED,
    period:Optional[str]=None,
    aggregation:Optional[List[MonitoringAggregation]]=None,
    include_webhook:bool=False,
):
    return (yield from self._monitoring_steps(
        '/scrape/monitoring/metrics',
        self._build_metrics_params(format, period, aggregation, include_webhook),
    ))
def get_monitoring_target_metrics(self,
domain: str,
group_subdomain: bool = False,
period: Literal['subscription', 'last7d', 'last24h', 'last1h', 'last5m'] | None = 'last24h',
start: datetime.datetime | None = None,
end: datetime.datetime | None = None,
include_webhook: bool = False)
Expand source code
@operation
def get_monitoring_target_metrics(
    self,
    domain:str,
    group_subdomain:bool=False,
    period:Optional[MonitoringTargetPeriod]=ScraperAPI.MONITORING_PERIOD_LAST_24H,
    start:Optional[datetime.datetime]=None,
    end:Optional[datetime.datetime]=None,
    include_webhook:bool=False,
):
    return (yield from self._monitoring_steps(
        '/scrape/monitoring/metrics/target',
        self._build_target_params(domain, group_subdomain, period, start, end, include_webhook),
    ))
def get_screenshot_monitoring_metrics(self,
format: str = 'structured',
period: str | None = None,
aggregation: List[Literal['account', 'project', 'target']] | None = None,
include_webhook: bool = False)
Expand source code
@operation
def get_screenshot_monitoring_metrics(
    self,
    format:str=ScraperAPI.MONITORING_DATA_FORMAT_STRUCTURED,
    period:Optional[str]=None,
    aggregation:Optional[List[MonitoringAggregation]]=None,
    include_webhook:bool=False,
):
    return (yield from self._monitoring_steps(
        '/screenshot/monitoring/metrics',
        self._build_metrics_params(format, period, aggregation, include_webhook),
    ))
def get_screenshot_monitoring_target_metrics(self,
domain: str,
group_subdomain: bool = False,
period: Literal['subscription', 'last7d', 'last24h', 'last1h', 'last5m'] | None = 'last24h',
start: datetime.datetime | None = None,
end: datetime.datetime | None = None,
include_webhook: bool = False)
Expand source code
@operation
def get_screenshot_monitoring_target_metrics(
    self,
    domain:str,
    group_subdomain:bool=False,
    period:Optional[MonitoringTargetPeriod]=ScraperAPI.MONITORING_PERIOD_LAST_24H,
    start:Optional[datetime.datetime]=None,
    end:Optional[datetime.datetime]=None,
    include_webhook:bool=False,
):
    return (yield from self._monitoring_steps(
        '/screenshot/monitoring/metrics/target',
        self._build_target_params(domain, group_subdomain, period, start, end, include_webhook),
    ))
def list_crawler_schedules(self, *, status: str | None = None) ‑> Steps[typing.List[typing.Dict[str, typing.Any]]]
Expand source code
@operation
def list_crawler_schedules(self, *, status: Optional[str] = None) -> Steps[List[Schedule]]:
    params = {"status": status} if status else None
    return (yield from self._schedule_steps("GET", "/crawl/schedules", query=params))
def list_scrape_schedules(self, *, status: str | None = None) ‑> Steps[typing.List[typing.Dict[str, typing.Any]]]
Expand source code
@operation
def list_scrape_schedules(self, *, status: Optional[str] = None) -> Steps[List[Schedule]]:
    params = {"status": status} if status else None
    return (yield from self._schedule_steps("GET", "/scrape/schedules", query=params))
def list_screenshot_schedules(self, *, status: str | None = None) ‑> Steps[typing.List[typing.Dict[str, typing.Any]]]
Expand source code
@operation
def list_screenshot_schedules(self, *, status: Optional[str] = None) -> Steps[List[Schedule]]:
    params = {"status": status} if status else None
    return (yield from self._schedule_steps("GET", "/screenshot/schedules", query=params))
def open(self)
Expand source code
def open(self):
    if self.http_session is None:
        self.http_session = Session()
        self.http_session.verify = self.verify
        self.http_session.timeout = (self.connect_timeout, self.default_read_timeout)
        self.http_session.params['key'] = self.key
        self.http_session.headers['accept-encoding'] = self.body_handler.content_encoding
        self.http_session.headers['accept'] = self.body_handler.accept
        self.http_session.headers['user-agent'] = self.ua
def pause_schedule(self, schedule_id: str) ‑> Steps[typing.Dict[str, typing.Any]]
Expand source code
@operation
def pause_schedule(self, schedule_id: str) -> Steps[Schedule]:
    return (yield from self._schedule_steps("POST", "/schedules/" + quote(schedule_id, safe="") + "/pause"))
def resilient_scrape(self,
scrape_config: ScrapeConfig,
retry_on_errors: Set[Exception] | None = None,
retry_on_status_code: List[int] | None = None,
tries: int = 5,
delay: int = 20) ‑> Steps[ScrapeApiResponse]
Expand source code
@operation
def resilient_scrape(
    self,
    scrape_config:ScrapeConfig,
    retry_on_errors:Optional[Set[Exception]]=None,
    retry_on_status_code:Optional[List[int]]=None,
    tries: int = 5,
    delay: int = 20,
) -> Steps[ScrapeApiResponse]:
    if retry_on_errors is None:
        retry_on_errors = {ScrapflyError}
    assert isinstance(retry_on_errors, set), 'retry_on_errors is not a set()'

    # Exponential backoff until `tries` attempts or `delay` seconds.
    deadline = time.monotonic() + delay

    for attempt in range(tries):
        try:
            try:
                return (yield from self._scrape(scrape_config))
            except (UpstreamHttpClientError, UpstreamHttpServerError) as e:
                if retry_on_status_code is not None and e.api_response and e.api_response.upstream_status_code not in retry_on_status_code:
                    return e.api_response

                raise
        except tuple(retry_on_errors):
            remaining = deadline - time.monotonic()

            if attempt + 1 == tries or remaining <= 0:
                raise

            yield Sleep(min(retry_delay(attempt), remaining))
def resume_schedule(self, schedule_id: str) ‑> Steps[typing.Dict[str, typing.Any]]
Expand source code
@operation
def resume_schedule(self, schedule_id: str) -> Steps[Schedule]:
    return (yield from self._schedule_steps("POST", "/schedules/" + quote(schedule_id, safe="") + "/resume"))

Inherited members