BeautifulSoup crawler
Get data from every page on a site. Good for simple sites like blogs, news, or product listings, but it can't run client-side JavaScript. Uses BeautifulSoup, Python's most popular HTML parser.
my_actor/main.py
my_actor/routes.py
my_actor/__main__.py
1"""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/python7"""8
9from __future__ import annotations10
11import asyncio12
13from apify import Actor, Event14from crawlee.crawlers import BeautifulSoupCrawler15
16from .routes import router17
18
19async def main() -> None:20 """Define a main entry point for the Apify Actor.21
22 This coroutine is executed using `asyncio.run()`, so it must remain an asynchronous function for proper execution.23 Asynchronous execution is required for communication with Apify platform, and it also enhances performance in24 the field of web scraping significantly.25 """26 # Enter the context of the Actor.27 async with Actor:28 # Handle graceful abort - Actor is being stopped by user or platform29 async def on_aborting() -> None:30 # Persist any state, do any cleanup you need, and terminate the Actor using31 # `await Actor.exit()` explicitly as soon as possible. This will help ensure that32 # the Actor is doing best effort to honor any potential limits on costs of a33 # single run set by the user.34 # Wait 1 second to allow Crawlee/SDK state persistence operations to complete35 # This is a temporary workaround until SDK implements proper state persistence in the aborting event36 await asyncio.sleep(1)37 await Actor.exit()38
39 Actor.on(Event.ABORTING, on_aborting)40
41 # Retrieve the Actor input, and use default values if not provided.42 actor_input = await Actor.get_input() or {}43 start_urls = [44 url.get('url')45 for url in actor_input.get(46 'start_urls',47 [{'url': 'https://apify.com'}],48 )49 ]50
51 # Exit if no start URLs are provided.52 if not start_urls:53 Actor.log.info('No start URLs specified in Actor input, exiting...')54 await Actor.exit()55
56 # Create a crawler.57 crawler = BeautifulSoupCrawler(58 # Limit the crawl to max requests. Remove or increase it for crawling all links.59 max_requests_per_crawl=10,60 # Set the request handler to the request router defined in routes.py.61 request_handler=router,62 )63
64 # Run the crawler with the starting requests.65 await crawler.run(start_urls)This template example was built with Crawlee for Python to scrape data from a website using Beautiful Soup wrapped into BeautifulSoupCrawler .
Once you've installed the dependencies, start the Actor:
$apify run
Once your Actor is ready, you can push it to the Apify Console:
apify login # first, you need to log in if you haven't already done soapify push
.actor/├── actor.json # Actor config: name, version, env vars, runtime settings├── dataset_schema.json # Structure and representation of data produced by an Actor├── input_schema.json # Input validation & Console form definition└── output_schema.json # Specifies where an Actor stores its outputsrc/└── main.py # Actor entry point and orchestratorstorage/ # Local storage (mirrors Cloud during development)├── datasets/ # Output items (JSON objects)├── key_value_stores/ # Files, config, INPUT└── request_queues/ # Pending crawl requestsDockerfile # Container image definition
For more information, see the Actor definition documentation.
This code is a Python script that uses BeautifulSoup to scrape data from a website. It then stores the website titles in a dataset.
- The crawler starts with URLs provided from the input
startUrlsfield defined by the input schema. Number of scraped pages is limited bymaxPagesPerCrawlfield from the input schema. - The crawler uses
requestHandlerfor each URL to extract the data from the page with the BeautifulSoup library and to save the title and URL of each page to the dataset. It also logs out each result that is being saved.
- Apify SDK - toolkit for building Actors
- Crawlee for Python - web scraping and browser automation library
- Input schema - define and easily validate a schema for your Actor's input
- Dataset - store structured data where each object stored has the same attributes
- Beautiful Soup - a library for pulling data out of HTML and XML files
- Proxy configuration - rotate IP addresses to prevent blocking
- Quick Start guide for building your first Actor
- Video introduction to Python SDK
- Webinar introducing to Crawlee for Python
- Apify Python SDK documentation
- Crawlee for Python documentation
- Python tutorials in Academy
- Integration with Zapier , Make, Google Drive and others
- Video guide on getting data using Apify API
Empty Python Actor
An Actor with the Apify SDK set up, so you can build any tool you need.
Python one-page scraper
Get data from one web page with BeautifulSoup. The simplest way to start scraping.
Python project managed by uv
A general-purpose Python Actor with its project and dependencies managed by the uv package manager. A minimal starting point for any use case.
Python multi-page scraper
Get data from multiple web pages with BeautifulSoup. Fast and light for simple sites.
Python Playwright scraper
A real-browser scraper that gets data HTTP scrapers miss. Good for social feeds, dashboards, or single-page apps.
Python Selenium scraper
A Chrome browser scraper that renders JavaScript before extracting data. Good for social feeds, dashboards, or single-page apps.