A simple asynchronous web crawler designed to scrape web pages and generate Markdown documents using the Crawl4AI functionality. This application fetches URLs from a sitemap, respects robots.txt, and saves scraped content as Markdown files.
The web crawling process follows a systematic approach:
-
URL Validation:
- The crawler begins by verifying the provided URL for correctness and checking for any potentially malicious data to ensure a safe crawling experience.
-
Checking for
robots.txt:- Once the URL is validated, the crawler retrieves the
robots.txtfile for the target website if the--check-robotsparameter is passed. This file specifies which parts of the site should not be crawled.
- Once the URL is validated, the crawler retrieves the
-
Retrieving Sitemap Information:
- The crawler checks for the presence of a sitemap URL within the
robots.txt. If a sitemap is provided, it follows that URL (in case it is not standard one assitemap.xml). - If no specific sitemap URL is found in the
robots.txt, the crawler appends "sitemap.xml" to the base URL and attempts to access it.
- The crawler checks for the presence of a sitemap URL within the
-
URL Collection from Sitemap:
- If URLs are obtained from the sitemap, those links are collected for crawling. If no URLs are found, the crawler resorts to scraping the website directly using Scrapy to construct a sitemap programmatically.
- If no URLs are found from the sitemap or scraping, the crawler defaults to crawling the current page.
-
Crawling Logic:
- If the
--crawl-allflag is specified, the crawler processes all pages collected from the sitemap in parallel, respecting the optional--max-pagesparameter to limit the total number of pages crawled. - If the
--crawl-allflag is not set, the crawler focuses on scraping only the current page.
- If the
-
Markdown Generation:
- The crawler uses Crawl4AI to scrape the content from each page and generate Markdown files.
- The generated Markdown files are saved in the
crawled_datadirectory.
-
Logging:
- The crawler provides logging to track the progress of the crawling process, including any encountered errors.
- Primary Fetching: Attempt to fetch URLs from the specified
sitemap.xmlif available. - Fallback Scraping: If the
sitemap.xmlis not available, directly scrape the provided URL for available links. - Respect for
robots.txt: The crawler adheres torobots.txtrules and filters out disallowed URLs. - Markdown Generation: Generates and saves Markdown files for each crawled page.
- Configurable Crawling Options: Options to crawl all pages or just the specified base URL, along with a configurable maximum number of pages to crawl.
- Logging: Includes logging for debugging and monitoring the crawling process.
Results from the crawling process are temporarily stored in a dedicated crawled_data directory at the root of the project. For each website processed, a unique folder is created to organize the collected data.
- Each folder is named according to a sanitized version of the website's URL.
- Within each website's folder, a JSON file named
crawl_metadata.jsonis generated. This file contains the following structure:
{
"url": "https://example.com/",
"markdown_file": "path/to/markdown/file"
}- From Command Line: Run the application from the command line using the following syntax:
python -m src.main <url> [--crawl-all] [--max-pages <number>] [--check-robots]- As a Module: You can directly import and call run_crawler() from any other module:
from src.main import run_crawler
asyncio.run(run_crawler("https://www.example.com/", crawl_all=True, max_pages=5))<url>: The base URL to crawl.--crawl-all: Optional flag to crawl all pages found in the sitemap and generate Markdown for each.--max-pages: Set the maximum number of pages to crawl (default is 50).--check-robots: Optional flag to check therobots.txtrules and filter out disallowed URLs.
To crawl a site and generate Markdown files for all URLs from the sitemap and respect the robots.txt rules:
python -m src.main https://example.com --crawl-all --max-pages 50 --check-robotsTo only crawl the specified base URL and generate a Markdown file:
python -m src.main https://example.comCrawled data will be saved as Markdown files in the crawled_data directory located in the parent directory of the script.
This application can also be run using Docker and Docker Compose.
To build the Docker image without using the cache, run the following command:
docker-compose build --no-cacheTo run the crawler within a Docker container, use the following command:
docker-compose run crawler https://example.com --crawl-all --max-pages 5In this command:
- Replace
https://example.comwith the base URL you want to crawl. - The
--crawl-allflag tells the crawler to fetch all pages found in the sitemap. - The
--max-pages5 argument limits the crawling process to a maximum of 5 pages.
The application integrates with Crawl4AI to create Markdown documents based on the scraped content from each page. The Markdown files will include relevant information extracted during the crawl.
The project includes test files that ensure the functionality of the web crawler is robust and reliable. You can run the tests to validate the implementation and behavior of the crawler. To run the tests, simply execute:
python -m unittest discover -s tests