メインコンテンツへスキップ

前提条件

SDK をインストール

mix.exsfirecrawl を追加します。
defp deps do
  [
    {:firecrawl, "~> 1.0"}
  ]
end
config/config.exs でAPIキーを設定します:
config :firecrawl, api_key: System.get_env("FIRECRAWL_API_KEY")

ウェブを検索する

{:ok, result} = Firecrawl.search_and_scrape(query: "firecrawl web scraping", limit: 5)

for entry <- result.body["data"]["web"] do
  IO.puts("#{entry["title"]} - #{entry["url"]}")
end

ページをスクレイピングする

{:ok, result} = Firecrawl.scrape_and_extract_from_url(url: "https://example.com")
IO.puts(result.body["data"]["markdown"])
{
  "success": true,
  "data": {
    "markdown": "# Example Domain\n\nThis domain is for use in illustrative examples...",
    "metadata": {
      "title": "Example Domain",
      "sourceURL": "https://example.com"
    }
  }
}

ページを操作する

ページをスクレイピングした後、ブラウザセッション API を使ってそのまま操作を続けます。
{:ok, scrape} = Firecrawl.scrape_and_extract_from_url(
  url: "https://www.amazon.com",
  formats: ["markdown"]
)

scrape_id = get_in(scrape.body, ["data", "metadata", "scrapeId"])

# インタラクト用のREST APIを使用する(プロンプトベース)
headers = [
  {"Authorization", "Bearer #{Application.get_env(:firecrawl, :api_key)}"},
  {"Content-Type", "application/json"}
]

{:ok, _} = Req.post(
  "https://api.firecrawl.dev/v2/scrape/#{scrape_id}/interact",
  json: %{prompt: "Search for iPhone 16 Pro Max"},
  headers: headers
)

{:ok, response} = Req.post(
  "https://api.firecrawl.dev/v2/scrape/#{scrape_id}/interact",
  json: %{prompt: "Click on the first result and tell me the price"},
  headers: headers
)

IO.inspect(response.body)

# セッションを停止する
Req.delete(
  "https://api.firecrawl.dev/v2/scrape/#{scrape_id}/interact",
  headers: headers
)

環境変数

ハードコードする代わりに、FIRECRAWL_API_KEY を環境変数として設定します:
export FIRECRAWL_API_KEY=fc-YOUR-API-KEY

次のステップ

スクレイピングのドキュメント

フォーマット、アクション、プロキシを含むスクレイピングの全オプション

検索ドキュメント

ウェブを検索し、ページ全体のコンテンツを取得

Interact ドキュメント

クリック、フォーム入力、動的コンテンツの抽出

Elixir SDK リファレンス

クロール、map、バッチスクレイプなどを含む SDK の完全なリファレンス