> ## Documentation Index
> Fetch the complete documentation index at: https://student-213fb9fc.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 增强模式

> 针对复杂站点使用增强代理，实现可靠抓取并保护隐私

Firecrawl 提供不同类型的代理服务，帮助你抓取不同复杂度的网站。可以使用 `proxy` 参数指定代理类型。

<div id="proxy-types">
  ### 代理类型
</div>

Firecrawl 支持三种代理类型：

* **basic**：适用于抓取大多数站点的代理。速度快，通常能正常工作。
* **enhanced**：用于在保持隐私的前提下抓取复杂站点的增强型代理。速度较慢，但在某些站点上更可靠。
* **auto**：如果使用 basic 代理抓取失败，Firecrawl 会自动切换为 enhanced 代理并重试抓取。如果使用 enhanced 重试成功，该次抓取将计费 5 个积分；如果首次使用 basic 即成功，则仅按常规费用计费。

如果你未指定代理，Firecrawl 将默认使用 auto。

<div id="using-enhanced-mode">
  ### 使用增强模式
</div>

在抓取复杂网站时，你可以使用增强模式（enhanced mode），在提高成功率的同时保护隐私。

<CodeGroup>
  ```python Python theme={null}
  from firecrawl import Firecrawl

  firecrawl = Firecrawl(api_key='fc-YOUR-API-KEY')

  # 选择代理类型：'basic' | 'enhanced' | 'auto'
  doc = firecrawl.scrape('https://example.com', formats=['markdown'], proxy='auto')

  print(doc.warning or 'ok')
  ```

  ```js Node theme={null}
  import Firecrawl from '@mendable/firecrawl-js';

  const firecrawl = new Firecrawl({ apiKey: "fc-YOUR-API-KEY" });

  // 选择代理类型：'basic' | 'enhanced' | 'auto'
  const doc = await firecrawl.scrape('https://example.com', {
    formats: ['markdown'],
    proxy: 'auto'
  });

  console.log(doc.warning || 'ok');
  ```

  ```bash cURL theme={null}

  // 选择代理类型：'basic' | 'enhanced' | 'auto'
  curl -X POST https://api.firecrawl.dev/v2/scrape \
      -H 'Content-Type: application/json' \
      -H 'Authorization: Bearer fc-YOUR-API-KEY' \
      -d '{
        "url": "https://example.com",
        "proxy": "auto"
      }'

  ```
</CodeGroup>

**注意：** 使用增强代理时，每个请求会消耗 5 点额度。

<div id="using-enhanced-as-a-retry-mechanism">
  ## 将 Enhanced 用作重试机制
</div>

一种常见模式是先使用默认代理设置进行抓取，如果在响应的 `metadata.statusCode` 字段中遇到特定错误状态码（401、403 或 500），再使用 enhanced 模式重试。这些状态码可能表明网站正在阻止你的请求。

<CodeGroup>
  ```python Python theme={null}
  # pip install firecrawl-py

  from firecrawl import Firecrawl

  firecrawl = Firecrawl(api_key="YOUR_API_KEY")

  # First try with basic proxy
  try:
      content = firecrawl.scrape("https://example.com")
      
      # Check if we got an error status code
      status_code = content.get("metadata", {}).get("statusCode")
      if status_code in [401, 403, 500]:
          print(f"收到状态码 {status_code},改用 enhanced 代理重试")
          # Retry with enhanced proxy
          content = firecrawl.scrape("https://example.com", proxy="enhanced")
      
      print(content["markdown"])
  except Exception as e:
      print(f"Error: {e}")
      # Retry with enhanced proxy on exception
      try:
          content = firecrawl.scrape("https://example.com", proxy="enhanced")
          print(content["markdown"])
      except Exception as e:
          print(f"Enhanced proxy also failed: {e}")
  ```

  ```js Node theme={null}
  // npm install @mendable/firecrawl-js

  import { Firecrawl } from '@mendable/firecrawl-js';

  const firecrawl = new Firecrawl({ apiKey: 'YOUR_API_KEY' });

  // Function to scrape with retry logic
  async function scrapeWithRetry(url) {
    try {
      // First try with default proxy
      const content = await firecrawl.scrape(url);
      
      // Check if we got an error status code
      const statusCode = content?.metadata?.statusCode;
      if ([401, 403, 500].includes(statusCode)) {
        console.log(`收到状态码 ${statusCode}，改用 enhanced 代理重试`);
        // 使用 enhanced 代理重试
        return await firecrawl.scrape(url, {
          proxy: 'enhanced'
        });
      }
      
      return content;
    } catch (error) {
      console.error(`Error: ${error.message}`);
      // Retry with enhanced proxy on exception
      try {
        return await firecrawl.scrape(url, {
          proxy: 'enhanced'
        });
      } catch (retryError) {
        console.error(`Enhanced proxy also failed: ${retryError.message}`);
        throw retryError;
      }
    }
  }

  // Usage
  const content = await scrapeWithRetry('https://example.com');
  console.log(content.markdown);
  ```

  ```bash cURL theme={null}
  # First try with default proxy
  RESPONSE=$(curl -s -X POST https://api.firecrawl.dev/v2/scrape \
      -H 'Content-Type: application/json' \
      -H 'Authorization: Bearer YOUR_API_KEY' \
      -d '{
        "url": "https://example.com"
      }')

  # Extract status code from response
  STATUS_CODE=$(echo $RESPONSE | jq -r '.data.metadata.statusCode')

  # Check if status code indicates we should retry with enhanced
  if [[ "$STATUS_CODE" == "401" || "$STATUS_CODE" == "403" || "$STATUS_CODE" == "500" ]]; then
      echo "收到状态码 $STATUS_CODE,改用 enhanced 代理重试"
      
      # Retry with enhanced proxy
      curl -X POST https://api.firecrawl.dev/v2/scrape \
          -H 'Content-Type: application/json' \
          -H 'Authorization: Bearer YOUR_API_KEY' \
          -d '{
            "url": "https://example.com",
            "proxy": "enhanced"
          }'
  else
      # Output the original response
      echo $RESPONSE
  fi
  ```
</CodeGroup>

这种方法可以让你通过仅在必要时使用 enhanced 模式来优化额度的使用。
