MCPcopy Create free account
hub / github.com/apify/crawlee-python / run

Method run

src/crawlee/crawlers/_basic/_basic_crawler.py:685–773  ·  view source on GitHub ↗

Run the crawler until all requests are processed. Args: requests: The requests to be enqueued before the crawler starts. purge_request_queue: If this is `True` and the crawler is not being run for the first time, the default request queue will be purg

(
        self,
        requests: Sequence[str | Request] | None = None,
        *,
        purge_request_queue: bool = True,
    )

Source from the content-addressed store, hash-verified

683 return callback
684
685 async def run(
686 self,
687 requests: Sequence[str | Request] | None = None,
688 *,
689 purge_request_queue: bool = True,
690 ) -> FinalStatistics:
691 """Run the crawler until all requests are processed.
692
693 Args:
694 requests: The requests to be enqueued before the crawler starts.
695 purge_request_queue: If this is `True` and the crawler is not being run for the first time, the default
696 request queue will be purged.
697 """
698 if self._running:
699 raise RuntimeError(
700 'This crawler instance is already running, you can add more requests to it via `crawler.add_requests()`'
701 )
702
703 self._running = True
704
705 if self._respect_robots_txt_file and not isinstance(self._request_manager, ThrottlingRequestManager):
706 self._logger.warning(
707 'The `respect_robots_txt_file` option is enabled, but the crawler is not using '
708 '`ThrottlingRequestManager`. Crawl-delay directives from robots.txt will not be enforced. To enable '
709 'crawl-delay support, configure the crawler to use `ThrottlingRequestManager` as the request manager.'
710 )
711
712 if self._has_finished_before:
713 await self._statistics.reset()
714
715 if self._use_session_pool:
716 await self._session_pool.reset_store()
717
718 if purge_request_queue:
719 request_manager = await self.get_request_manager()
720 await request_manager.purge()
721
722 if requests is not None:
723 await self.add_requests(requests)
724
725 interrupted = False
726
727 def sigint_handler() -> None:
728 nonlocal interrupted
729
730 if not interrupted:
731 interrupted = True
732 self._logger.info('Pausing... Press CTRL+C again to force exit.')
733
734 run_task.cancel()
735
736 run_task = asyncio.create_task(self._run_crawler(), name='run_crawler_task')
737
738 if threading.current_thread() is threading.main_thread(): # `add_signal_handler` works only in the main thread
739 with suppress(NotImplementedError): # event loop signal handlers are not supported on Windows
740 asyncio.get_running_loop().add_signal_handler(signal.SIGINT, sigint_handler)
741
742 try:

Calls 10

get_request_managerMethod · 0.95
add_requestsMethod · 0.95
_run_crawlerMethod · 0.95
_save_crawler_stateMethod · 0.95
reset_storeMethod · 0.80
calculateMethod · 0.80
to_tableMethod · 0.80
to_dictMethod · 0.80
resetMethod · 0.45
purgeMethod · 0.45