Back to template gallery

Crawlee + Playwright + Camoufox

Crawl and scrape websites using Crawlee and Playwright with Camoufox. Start from a URL and store results to your Apify dataset.

Language

python

Tools

crawlee

playwright

Use cases

Starter

Web scraping

my_actor/main.py

my_actor/routes.py

my_actor/__main__.py

1from apify import Actor
2from camoufox import AsyncNewBrowser
3from crawlee.browsers import BrowserPool, PlaywrightBrowserController, PlaywrightBrowserPlugin
4from crawlee.crawlers import PlaywrightCrawler
5from typing_extensions import override
6
7from .routes import router
8
9
10class CamoufoxPlugin(PlaywrightBrowserPlugin):
11 """Browser plugin that uses Camoufox Browser, but otherwise keeps the functionality of PlaywrightBrowserPlugin."""
12
13 @override
14 async def new_browser(self) -> PlaywrightBrowserController:
15 if not self._playwright:
16 raise RuntimeError('Playwright browser plugin is not initialized.')
17
18 return PlaywrightBrowserController(
19 browser=await AsyncNewBrowser(self._playwright, headless=True),
20 max_open_pages_per_browser=1, # Increase, if camoufox can handle it in your use case.
21 header_generator=None, # This turns off the crawlee header_generation. Camoufox has its own.
22 )
23
24
25async def main() -> None:
26 """Define a main entry point for the Apify Actor.
27
28 This coroutine is executed using `asyncio.run()`, so it must remain an asynchronous function for proper execution.
29 Asynchronous execution is required for communication with Apify platform, and it also enhances performance in
30 the field of web scraping significantly.
31 """
32 # Enter the context of the Actor.
33 async with Actor:
34 # Retrieve the Actor input, and use default values if not provided.
35 actor_input = await Actor.get_input() or {}
36 start_urls = [
37 url.get('url')
38 for url in actor_input.get(
39 'start_urls',
40 [{'url': 'https://apify.com'}],
41 )
42 ]
43
44 # Exit if no start URLs are provided.
45 if not start_urls:
46 Actor.log.info('No start URLs specified in Actor input, exiting...')
47 await Actor.exit()
48
49 # Create a crawler.
50 crawler = PlaywrightCrawler(
51 # Limit the crawl to max requests. Remove or increase it for crawling all links.
52 max_requests_per_crawl=10,
53 browser_pool=BrowserPool(plugins=[CamoufoxPlugin()]),
54 # Set the request handler to the request router defined in routes.py.
55 request_handler=router,
56 )
57
58 # Run the crawler with the starting requests.
59 await crawler.run(start_urls)

Python Crawlee with Playwright template with Camoufox

A template for web scraping  data from websites starting from provided URLs using Python. The starting URLs are passed through the Actor's input schema, defined by the input schema . The template uses Crawlee for Python  for efficient web crawling, making requests via headless browser managed by Playwright , and handling each request through a user-defined handler that uses Playwright  API to extract data from the page. Enqueued URLs are managed in the request queue , and the extracted data is saved in a dataset  for easy access. It uses Camoufox  - a stealthy fork of Firefox - preinstalled. Note that Camoufox might consume more resources than the default Playwright-bundled Chromium or Firefox.

Included features

Resources

Already have a solution in mind?

Sign up for a free Apify account and deploy your code to the platform in just a few minutes! If you want a head start without coding it yourself, browse our Store of existing solutions.