Skip to main content

Export entire dataset to file

This example demonstrates how to use the export_data() method of the crawler to export the entire default dataset to a single file. This method supports exporting data in either CSV or JSON format.


For these examples, we are using the BeautifulSoupCrawler. However, the same method is available for the PlaywrightCrawler as well. You can use it in exactly the same way.

import asyncio

from crawlee.beautifulsoup_crawler import BeautifulSoupCrawler, BeautifulSoupCrawlingContext

async def main() -> None:
crawler = BeautifulSoupCrawler(
# Limit the crawl to max requests. Remove or increase it for crawling all links.

# Define the default request handler, which will be called for every request.
async def request_handler(context: BeautifulSoupCrawlingContext) -> None:'Processing {context.request.url} ...')

# Extract data from the page.
data = {
'url': context.request.url,
'title': context.soup.title.string if context.soup.title else None,

# Enqueue all links found on the page.
await context.enqueue_links()

# Push the extracted data to the default dataset.
await context.push_data(data)

# Run the crawler with the initial list of URLs.

# Export the entire dataset to a JSON file.
await crawler.export_data('results.json')

if __name__ == '__main__':