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

前提条件

セットアップ

main.ts を作成します。
import Firecrawl from "npm:@mendable/firecrawl-js";

const firecrawl = new Firecrawl({
  apiKey: Deno.env.get("FIRECRAWL_API_KEY"),
});

Webを検索

Webを検索し、ページ全文を含む結果を返す /search ルートを追加します。
Deno.serve(async (req) => {
  const url = new URL(req.url);

  if (req.method === "POST" && url.pathname === "/search") {
    const { query } = await req.json();
    const results = await firecrawl.search(query, { limit: 5 });
    return Response.json(results);
  }

  return new Response("Not found", { status: 404 });
});

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

任意のURLからクリーンなMarkdownを抽出するため、/scrape ルートを追加します。
if (req.method === "POST" && url.pathname === "/scrape") {
  const { url: targetUrl } = await req.json();
  const result = await firecrawl.scrape(targetUrl);
  return Response.json(result);
}

ページをInteractする

/interact ルートを追加すると、実行中のブラウザセッションをInteractできます。ボタンのクリック、フォームへの入力、動的コンテンツの抽出が可能です。
if (req.method === "POST" && url.pathname === "/interact") {
  const result = await firecrawl.scrape("https://www.amazon.com", {
    formats: ["markdown"],
  });
  const scrapeId = result.metadata?.scrapeId;

  await firecrawl.interact(scrapeId, {
    prompt: "Search for iPhone 16 Pro Max",
  });
  const response = await firecrawl.interact(scrapeId, {
    prompt: "Click on the first result and tell me the price",
  });
  console.log(response.output);

  await firecrawl.stopInteraction(scrapeId);
  return Response.json({ output: response.output });
}

ローカルで実行

FIRECRAWL_API_KEY=fc-YOUR-API-KEY deno run --allow-net --allow-env main.ts

デプロイ

Deno Deploy CLI (deployctl) をインストールして、デプロイします。
deployctl deploy --project=my-scraper main.ts
Deno Deploy のダッシュボードまたは CLI で環境変数を設定します。
deployctl env set FIRECRAWL_API_KEY=fc-YOUR-API-KEY --project=my-scraper

試す

curl -X POST https://my-scraper.deno.dev/search \
  -H "Content-Type: application/json" \
  -d '{"query": "firecrawl web scraping"}'

次のステップ

Search のドキュメント

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

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

フォーマット、アクション、プロキシなど、すべてのスクレイピングのオプション

Interact のドキュメント

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

Node SDK リファレンス

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