Python · Web Crawling
Crawl a Website and Export Every Page URL and Title to JSON
Give the crawler one start URL; it follows every internal link in a real Chromium browser and writes url, title and a content snippet per page to results.json.
PythonCrawleePlaywrightasyncio
Abstract
The problem
Collecting every page of a help centre or documentation site by hand means endless clicking and copy-pasting.
The solution
A PlaywrightCrawler starts at one URL, calls enqueue_links() on every page to follow same-site links, and stores URL, title and the first 100 characters of HTML in a dataset.
The result
A results.json inventory of the whole site plus a printed page count.
Input & Output
Input
- One start URL in crawler.run([...])
Output
- results.json - one record per crawled page
Optional output
- Crawlee storage/ folder with datasets and request queues
- Page count printed to the console
System Diagram
Start URL
crawler.run([...])
Playwright page
Chromium renders JS
enqueue_links()
Follow same-site links
push_data()
url · title · snippet
results.json
export_data()
How It Works
Real browser
Playwright renders JavaScript-heavy pages that plain HTTP scrapers miss.
Automatic link following
enqueue_links() queues new same-domain links, so the crawl widens by itself.
Simple dataset export
Everything collected is exported to results.json with one call.
Adjustable limits
Set max_requests_per_crawl to cap the crawl, or switch headless on.
Configuration
| Option | Purpose |
|---|---|
headless | False shows the browser window; True runs invisibly. |
browser_type | Browser engine, 'chromium' by default. |
max_requests_per_crawl | Optional cap to avoid huge crawls (commented out). |
How to Run
pip install -r requirements.txtplaywright install chromiumpython link_crawler.pySource Code
# Remove the max_requests_per_crawl limit to crawl all pages found.
# Be cautious with this setting on large websites as it could lead to very long crawls or unintended load.
from crawlee.crawlers import PlaywrightCrawler, PlaywrightCrawlingContext
import asyncio # Import asyncio for the main function execution
async def main() -> None:
crawler = PlaywrightCrawler(
# max_requests_per_crawl=5, # Removed this line to crawl all links found
headless=False, # Show the browser window.
browser_type='chromium', # Use the Chromium browser (Chrome).
)
# Define the default request handler, which will be called for every request.
@crawler.router.default_handler
async def request_handler(context: PlaywrightCrawlingContext) -> None:
context.log.info(f'Processing {context.request.url} ...')
# Extract and enqueue all links found on the page.
# By default, enqueue_links will find <a> tags and add their hrefs to the queue.
# It respects the crawler's internal domain restrictions to avoid going off-topic.
await context.enqueue_links()
# Extract data from the page using Playwright API.
# This data includes the URL, title, and the first 100 characters of the page content.
data = {
'url': context.request.url,
'title': await context.page.title(),
'content': (await context.page.content())[:100], # First 100 chars of HTML content
}
# Push the extracted data to the default dataset.
await context.push_data(data)
# Run the crawler with the initial list of URLs.
# It will start here and follow links discovered by enqueue_links().
#await crawler.run(['https://crawlee.dev'])
await crawler.run(['https://example.com'])
# Export the entire dataset containing data from all crawled pages to a JSON file.
await crawler.export_data('results.json')
# Or work with the data directly.
data = await crawler.get_data()
print(f'Total pages crawled: {len(data.items)}')
# Be careful with large datasets, printing all might be slow or clutter the output
# print('Extracted ')
# for item in data.items:
# print(item) # Print each item's data
# Run the main function using asyncio
if __name__ == '__main__':
asyncio.run(main())Released under the MIT license. Free to use, modify and share.
Impact & Benefits
Frequently Asked Questions
How do I crawl all links on a website with Python?
Use Crawlee PlaywrightCrawler and call enqueue_links() in the request handler; the crawler follows same-site links automatically.
Can I limit how many pages it crawls?
Yes. Uncomment max_requests_per_crawl.
Is it polite to crawl any website?
Crawl only sites you are allowed to, respect robots.txt and rate limits.
Related
Bulk-Save Shared Terabox Links with a Selenium Scraper and GUI
Selenium scraper plus GUI automator that bulk-saves hundreds of shared Terabox links to a private account.
Read documentation