> ## Documentation Index
> Fetch the complete documentation index at: https://docs.evocrawl.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Cloudflare Workers

> 使用 Evocrawl 与 Cloudflare Workers，在边缘环境中搜索、抓取并与网页数据交互。

<div id="prerequisites">
  ## 准备工作
</div>

* Wrangler CLI (`npm install -g wrangler`)
* EvoCrawl API 密钥——[免费获取](https://www.evocrawl.com/app/api-keys)

<div id="setup">
  ## 设置
</div>

```bash theme={null}
npm create cloudflare@latest my-scraper
cd my-scraper
npm install @mendable/evocrawl-js
```

将你的 API 密钥添加为 Secret：

```bash theme={null}
wrangler secret put EVOCRAWL_API_KEY
```

<div id="search-the-web">
  ## 进行网页搜索
</div>

创建一个处理函数，用于进行网页搜索，并返回包含完整页面内容的结果。

编辑 `src/index.ts`：

```typescript theme={null}
import Evocrawl from "@mendable/evocrawl-js";

export interface Env {
  EVOCRAWL_API_KEY: string;
}

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const evocrawl = new Evocrawl({ apiKey: env.EVOCRAWL_API_KEY });
    const url = new URL(request.url);

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

    return new Response("未找到", { status: 404 });
  },
};
```

<div id="scrape-a-page">
  ## 抓取网页
</div>

添加一个 `/scrape` 路由，用于从任意 URL 提取干净的 Markdown。

```typescript theme={null}
if (request.method === "POST" && url.pathname === "/scrape") {
  const { url: targetUrl } = (await request.json()) as { url: string };
  const result = await evocrawl.scrape(targetUrl);
  return Response.json(result);
}
```

<div id="interact-with-a-page">
  ## 与页面交互
</div>

添加 `/interact` 路由以控制实时浏览器会话——点击按钮、填写表单，并提取动态内容。

```typescript theme={null}
if (request.method === "POST" && url.pathname === "/interact") {
  const result = await evocrawl.scrape("https://www.amazon.com", {
    formats: ["markdown"],
  });
  const scrapeId = result.metadata?.scrapeId;

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

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

<div id="deploy">
  ## 部署
</div>

```bash theme={null}
wrangler deploy
```

<div id="test-it">
  ## 试一试
</div>

```bash theme={null}
curl -X POST https://my-scraper.<your-subdomain>.workers.dev/search \
  -H "Content-Type: application/json" \
  -d '{"query": "evocrawl web scraping"}'
```

<div id="next-steps">
  ## 后续步骤
</div>

<CardGroup cols={2}>
  <Card title="搜索文档" icon="magnifying-glass" href="/zh/features/search">
    进行网页搜索并获取完整页面内容
  </Card>

  <Card title="抓取 文档" icon="file-lines" href="/zh/features/scrape">
    包含所有 scrape 选项，包括 formats、actions 和代理
  </Card>

  <Card title="交互文档" icon="hand-pointer" href="/zh/features/interact">
    点击、填写表单并提取动态内容
  </Card>

  <Card title="Node SDK 参考" icon="node" href="/zh/sdks/node">
    完整的 SDK 参考，涵盖爬取、map、batch scrape 等功能
  </Card>
</CardGroup>
