Playwright + Chrome
Crawler example that uses headless Chrome driven by Playwright to scrape a website. Headless browsers render JavaScript and can help when getting blocked.
src/main.py
src/__main__.py
1"""This module defines the main entry point for the Apify Actor.
2
3Feel free to modify this file to suit your specific needs.
4
5To build Apify Actors, utilize the Apify SDK toolkit, read more at the official documentation:
6https://docs.apify.com/sdk/python
7"""
8
9from urllib.parse import urljoin
10
11from playwright.async_api import async_playwright
12
13from apify import Actor, Request
14
15# Note: To run this Actor locally, ensure that Playwright browsers are installed.
16# Run `playwright install --with-deps` in the Actor's virtual environment to install them.
17# When running on the Apify platform, these dependencies are already included
18# in the Actor's Docker image.
19
20
21async def main() -> None:
22 """Main entry point for the Apify Actor.
23
24 This coroutine is executed using `asyncio.run()`, so it must remain an asynchronous function for proper execution.
25 Asynchronous execution is required for communication with Apify platform, and it also enhances performance in
26 the field of web scraping significantly.
27 """
28 # Enter the context of the Actor.
29 async with Actor:
30 # Retrieve the Actor input, and use default values if not provided.
31 actor_input = await Actor.get_input() or {}
32 start_urls = actor_input.get('start_urls', [{'url': 'https://apify.com'}])
33 max_depth = actor_input.get('max_depth', 1)
34
35 # Exit if no start URLs are provided.
36 if not start_urls:
37 Actor.log.info('No start URLs specified in actor input, exiting...')
38 await Actor.exit()
39
40 # Open the default request queue for handling URLs to be processed.
41 request_queue = await Actor.open_request_queue()
42
43 # Enqueue the start URLs with an initial crawl depth of 0.
44 for start_url in start_urls:
45 url = start_url.get('url')
46 Actor.log.info(f'Enqueuing {url} ...')
47 new_request = Request.from_url(url, user_data={'depth': 0})
48 await request_queue.add_request(new_request)
49
50 Actor.log.info('Launching Playwright...')
51
52 # Launch Playwright and open a new browser context.
53 async with async_playwright() as playwright:
54 # Configure the browser to launch in headless mode as per Actor configuration.
55 browser = await playwright.chromium.launch(
56 headless=Actor.config.headless,
57 args=['--disable-gpu'],
58 )
59 context = await browser.new_context()
60
61 # Process the URLs from the request queue.
62 while request := await request_queue.fetch_next_request():
63 url = request.url
64
65 if not isinstance(request.user_data['depth'], (str, int)):
66 raise TypeError('Request.depth is an enexpected type.')
67
68 depth = int(request.user_data['depth'])
69 Actor.log.info(f'Scraping {url} (depth={depth}) ...')
70
71 try:
72 # Open a new page in the browser context and navigate to the URL.
73 page = await context.new_page()
74 await page.goto(url)
75
76 # If the current depth is less than max_depth, find nested links
77 # and enqueue them.
78 if depth < max_depth:
79 for link in await page.locator('a').all():
80 link_href = await link.get_attribute('href')
81 link_url = urljoin(url, link_href)
82
83 if link_url.startswith(('http://', 'https://')):
84 Actor.log.info(f'Enqueuing {link_url} ...')
85 new_request = Request.from_url(
86 link_url,
87 user_data={'depth': depth + 1},
88 )
89 await request_queue.add_request(new_request)
90
91 # Extract the desired data.
92 data = {
93 'url': url,
94 'title': await page.title(),
95 }
96
97 # Store the extracted data to the default dataset.
98 await Actor.push_data(data)
99
100 except Exception:
101 Actor.log.exception(f'Cannot extract data from {url}.')
102
103 finally:
104 await page.close()
105 # Mark the request as handled to ensure it is not processed again.
106 await request_queue.mark_request_as_handled(request)
Python Playwright template
Included features
- Apify SDK for Python - a toolkit for building Apify Actors and scrapers in Python
- Input schema - define and easily validate a schema for your Actor's input
- Request queue - queues into which you can put the URLs you want to scrape
- Dataset - store structured data where each object stored has the same attributes
- Playwright - a browser automation library
Resources
- Playwright for web scraping in 2023
- Scraping single-page applications with Playwright
- How to scale Puppeteer and Playwright
- Integration with Zapier, Make, GitHub, Google Drive and other apps
- Video guide on getting data using Apify API
- A short guide on how to build web scrapers using code templates:
Scrape single page with provided URL with HTTPX and extract data from page's HTML with Beautiful Soup.
Example of a web scraper that uses Python HTTPX to scrape HTML from URLs provided on input, parses it using BeautifulSoup and saves results to storage.
Scraper example built with Selenium and headless Chrome browser to scrape a website and save the results to storage. A popular alternative to Playwright.
Empty template with basic structure for the Actor with Apify SDK that allows you to easily add your own functionality.
Template with basic structure for an Actor using Standby mode that allows you to easily add your own functionality.
This example Scrapy spider scrapes page titles from URLs defined in input parameter. It shows how to use Apify SDK for Python and Scrapy pipelines to save results.