Skip to main content

Python SDK for Firecrawl API

Project description

Firecrawl Python SDK

The Firecrawl Python SDK is a library that allows you to easily scrape and crawl websites, and output the data in a format ready for use with language models (LLMs). It provides a simple and intuitive interface for interacting with the Firecrawl API.

Installation

To install the Firecrawl Python SDK, you can use pip:

pip install firecrawl-py

Usage

  1. Get an API key from firecrawl.dev
  2. Set the API key as an environment variable named FIRECRAWL_API_KEY or pass it as a parameter to the FirecrawlApp class.

Here's an example of how to use the SDK:

from firecrawl import FirecrawlApp, ScrapeOptions

app = FirecrawlApp(api_key="fc-YOUR_API_KEY")

# Scrape a website:
data = app.scrape_url(
  'https://firecrawl.dev', 
  formats=['markdown', 'html']
)
print(data)

# Crawl a website:
crawl_status = app.crawl_url(
  'https://firecrawl.dev', 
  limit=100, 
  scrape_options=ScrapeOptions(formats=['markdown', 'html'])
)
print(crawl_status)

Scraping a URL

To scrape a single URL, use the scrape_url method. It takes the URL as a parameter and returns the scraped data as a dictionary.

# Scrape a website:
scrape_result = app.scrape_url('firecrawl.dev', formats=['markdown', 'html'])
print(scrape_result)

Crawling a Website

To crawl a website, use the crawl_url method. It takes the starting URL and optional parameters as arguments. The params argument allows you to specify additional options for the crawl job, such as the maximum number of pages to crawl, allowed domains, and the output format.

crawl_status = app.crawl_url(
  'https://firecrawl.dev', 
  limit=100, 
  scrape_options=ScrapeOptions(formats=['markdown', 'html']),
  poll_interval=30
)
print(crawl_status)

Asynchronous Crawling

Looking for async operations? Check out the Async Class section below.

To crawl a website asynchronously, use the crawl_url_async method. It returns the crawl ID which you can use to check the status of the crawl job. It takes the starting URL and optional parameters as arguments. The params argument allows you to specify additional options for the crawl job, such as the maximum number of pages to crawl, allowed domains, and the output format.

crawl_status = app.async_crawl_url(
  'https://firecrawl.dev', 
  limit=100, 
  scrape_options=ScrapeOptions(formats=['markdown', 'html']),
)
print(crawl_status)

Checking Crawl Status

To check the status of a crawl job, use the check_crawl_status method. It takes the job ID as a parameter and returns the current status of the crawl job.

crawl_status = app.check_crawl_status("<crawl_id>")
print(crawl_status)

Cancelling a Crawl

To cancel an asynchronous crawl job, use the cancel_crawl method. It takes the job ID of the asynchronous crawl as a parameter and returns the cancellation status.

cancel_crawl = app.cancel_crawl(id)
print(cancel_crawl)

Map a Website

Use map_url to generate a list of URLs from a website. The params argument let you customize the mapping process, including options to exclude subdomains or to utilize the sitemap.

# Map a website:
map_result = app.map_url('https://firecrawl.dev')
print(map_result)

{/* ### Extracting Structured Data from Websites

To extract structured data from websites, use the extract method. It takes the URLs to extract data from, a prompt, and a schema as arguments. The schema is a Pydantic model that defines the structure of the extracted data.

*/}

Crawling a Website with WebSockets

To crawl a website with WebSockets, use the crawl_url_and_watch method. It takes the starting URL and optional parameters as arguments. The params argument allows you to specify additional options for the crawl job, such as the maximum number of pages to crawl, allowed domains, and the output format.

# inside an async function...
nest_asyncio.apply()

# Define event handlers
def on_document(detail):
    print("DOC", detail)

def on_error(detail):
    print("ERR", detail['error'])

def on_done(detail):
    print("DONE", detail['status'])

    # Function to start the crawl and watch process
async def start_crawl_and_watch():
    # Initiate the crawl job and get the watcher
    watcher = app.crawl_url_and_watch('firecrawl.dev', exclude_paths=['blog/*'], limit=5)

    # Add event listeners
    watcher.add_event_listener("document", on_document)
    watcher.add_event_listener("error", on_error)
    watcher.add_event_listener("done", on_done)

    # Start the watcher
    await watcher.connect()

# Run the event loop
await start_crawl_and_watch()

Error Handling

The SDK handles errors returned by the Firecrawl API and raises appropriate exceptions. If an error occurs during a request, an exception will be raised with a descriptive error message.

Async Class

For async operations, you can use the AsyncFirecrawlApp class. Its methods are the same as the FirecrawlApp class, but they don't block the main thread.

from firecrawl import AsyncFirecrawlApp

app = AsyncFirecrawlApp(api_key="YOUR_API_KEY")

# Async Scrape
async def example_scrape():
  scrape_result = await app.scrape_url(url="https://example.com")
  print(scrape_result)

# Async Crawl
async def example_crawl():
  crawl_result = await app.crawl_url(url="https://example.com")
  print(crawl_result)

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

firecrawl_py-2.9.0.tar.gz (38.9 kB view details)

Uploaded Source

Built Distribution

firecrawl_py-2.9.0-py3-none-any.whl (73.4 kB view details)

Uploaded Python 3

File details

Details for the file firecrawl_py-2.9.0.tar.gz.

File metadata

  • Download URL: firecrawl_py-2.9.0.tar.gz
  • Upload date:
  • Size: 38.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.5

File hashes

Hashes for firecrawl_py-2.9.0.tar.gz
Algorithm Hash digest
SHA256 075e17684d8489a06cc9ca62f04d6e4ec808d7341f54c3b429b68ce6a689f95e
MD5 650535b81a5c8bbd374393a7c7bb03de
BLAKE2b-256 639712a06c76c4796e2d9fe49e536c6b77bb5c2871e087cc9c351a9cc1d58bf7

See more details on using hashes here.

File details

Details for the file firecrawl_py-2.9.0-py3-none-any.whl.

File metadata

  • Download URL: firecrawl_py-2.9.0-py3-none-any.whl
  • Upload date:
  • Size: 73.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.5

File hashes

Hashes for firecrawl_py-2.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7772915ef14b89500d6f07b83693b575c12b63f064d8bb412723e9c516fbf2da
MD5 4376e5721dbdc2d1db6b5fe2862fe88f
BLAKE2b-256 07fab5d3e60ba476c631e44df497c946906c38f3fbe8987137e0fcfa801089f2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page