Python
Firecrawl Python SDK is a wrapper around the Firecrawl API to help you easily turn websites into markdown.
Installation
To install the Firecrawl Python SDK, you can use pip:
Usage
- Get an API key from firecrawl.dev
- Set the API key as an environment variable named
FIRECRAWL_API_KEY
or pass it as a parameter to theFirecrawlApp
class.
Here’s an example of how to use the SDK:
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.
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.
Asynchronous Crawling
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.
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.
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.
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.
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.
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.