AI爬虫黑科技 firecrawl本地部署

昨日trace还是博主的小甜甜, 急不可耐的使用它写了几个爬虫,今日看到Dify上部署智能体,使用firecrawl, 瞬间就把trace当做牛夫人。
在大模型的时代,企业知识库RAG系统如雨后春笋,而实时主学习的利器:AI爬虫,一键将网页内容转换为LLM-ready的数据。先搞搞:firecrawl。

一、Firecrawl 是什么

FireCrawl是一款创新的爬虫工具,它能够无需站点地图,抓取任何网站的所有可访问子页面。与传统爬虫工具相比,FireCrawl特别擅长处理使用JavaScript动态生成内容的网站,并且可以转换为LLM-ready的数据。
git官网:

文档:
在这里插入图片描述

此外,它还提供了一个易于使用的API,让开发者能够轻松实现内容的爬取和转换。

可以先通过FireCrawl 提供的playground来快速体验下。

在这里插入图片描述

最简单的情况下,只需要填一个URL就可以,firecrawl会抓取到相关的内容,还可以通过LLM来提取信息。使用firecrawl的在线服务是需要付费的,免费的只有500credit,所以接下来我们看下如何自己本地运行。

二、安装

2.1 单独安装

项目的介绍中说了,目前还在早期阶段,不建议自己部署,如果想要跑可以源码启动。首先需要准备本地的环境:

  1. 下载源码
    使用git或下载压缩包
git clone https://github.com/mendableai/firecrawl.git
  1. 设置环境参数
    在根目录中创建一个 .env,可以复制 apps/api/.env.example 中的模板
cd /firecrawl/apps/api
cp .env.example .env

#window
copy  .env.example .env

默认不会设置身份验证或任何可选子服务(pdf 解析、JS 阻止支持、AI 功能)

# .env

# ===== Required ENVS ======
NUM_WORKERS_PER_QUEUE=8 
PORT=3002
HOST=0.0.0.0

#for self-hosting using docker, use redis://redis:6379. For running locally, use redis://localhost:6379
REDIS_URL=redis://redis:6379

#for self-hosting using docker, use redis://redis:6379. For running locally, use redis://localhost:6379
REDIS_RATE_LIMIT_URL=redis://redis:6379 
XXXX

安装依赖项
使用 pnpm 安装依赖项

# cd apps/api # 确保您位于正确的文件夹中
pnpm install # 确保您拥有 pnpm 版本 9+!

运行项目
您需要打开 3 个终端来运行服务。

  • 终端 1 - 设置 redis

在项目中的任何位置运行命令

redis-server

博主局域网已安装redis, 配置/apps/api/.env中的redis,故不需执行启动redis
在这里插入图片描述

  • 终端 2 - 设置工作程序

现在,导航到 apps/api/ 目录并运行:

pnpm run workers

在这里插入图片描述

如果您要使用 llm-extract 功能,您还应该导出 OPENAI_API_KEY=sk-______

这将启动负责处理抓取作业的工作程序。

  • 终端 3 - 设置主服务器

为此,请导航到 apps/api/ 目录。如果您尚未安装 pnpm,可以在此处安装:https://pnpm.io/installation

接下来,使用以下命令运行您的服务器:

pnpm run start

在这里插入图片描述

测试
curl -X GET http://localhost:3002/test在这里插入图片描述

测试下载

curl -X POST http://localhost:3002/v1/crawl \
    -H 'Content-Type: application/json' \
    -d '{
      "url": "https://docs.firecrawl.dev"
    }'

也可以访问 http://localhost:3002/admin/@/queues/ ,查看firecrawl的管理后台。
在这里插入图片描述

在这里插入图片描述

2.2 自托管

下载源码
使用git或下载压缩包

git clone https://github.com/mendableai/firecrawl.git

设置环境参数
在根目录中创建一个 .env,可以复制 apps/api/.env.example 中的模板

cd /firecrawl/apps/api
cp .env.example .env

默认不会设置身份验证或任何可选子服务(pdf 解析、JS 阻止支持、AI 功能)

# .env

# ===== Required ENVS ======
NUM_WORKERS_PER_QUEUE=8 
PORT=3002
HOST=0.0.0.0

#for self-hosting using docker, use redis://redis:6379. For running locally, use redis://localhost:6379
REDIS_URL=redis://redis:6379

#for self-hosting using docker, use redis://redis:6379. For running locally, use redis://localhost:6379
REDIS_RATE_LIMIT_URL=redis://redis:6379 
PLAYWRIGHT_MICROSERVICE_URL=http://playwright-service:3000/html

## To turn on DB authentication, you need to set up supabase. 
USE_DB_AUTHENTICATION=false   

# ===== Optional ENVS ======

# Supabase Setup (used to support DB authentication, advanced logging, etc.)
SUPABASE_ANON_TOKEN= 
SUPABASE_URL= 
SUPABASE_SERVICE_TOKEN=

# Other Optionals
# use if you've set up authentication and want to test with a real API key
TEST_API_KEY=
# set if you'd like to test the scraping rate limit
RATE_LIMIT_TEST_API_KEY_SCRAPE=
# set if you'd like to test the crawling rate limit
RATE_LIMIT_TEST_API_KEY_CRAWL=
# set if you'd like to use scraping Be to handle JS blocking
SCRAPING_BEE_API_KEY=
# add for LLM dependednt features (image alt generation, etc.)
OPENAI_API_KEY=
BULL_AUTH_KEY=@
# use if you're configuring basic logging with logtail
LOGTAIL_KEY=
# set if you have a llamaparse key you'd like to use to parse pdfs
LLAMAPARSE_API_KEY=
# set if you'd like to send slack server health status messages
SLACK_WEBHOOK_URL=
# set if you'd like to send posthog events like job logs
POSTHOG_API_KEY=
# set if you'd like to send posthog events like job logs
POSTHOG_HOST=

# set if you'd like to use the fire engine closed beta
FIRE_ENGINE_BETA_URL=

# Proxy Settings for Playwright (Alternative you can can use a proxy service like oxylabs, which rotates IPs for you on every request)
PROXY_SERVER=
PROXY_USERNAME=
PROXY_PASSWORD=
# set if you'd like to block media requests to save proxy bandwidth
BLOCK_MEDIA=

# Set this to the URL of your webhook when using the self-hosted version of FireCrawl
SELF_HOSTED_WEBHOOK_URL=

# Resend API Key for transactional emails
RESEND_API_KEY=

# LOGGING_LEVEL determines the verbosity of logs that the system will output.
# Available levels are:
# NONE - No logs will be output.
# ERROR - For logging error messages that indicate a failure in a specific operation.
# WARN - For logging potentially harmful situations that are not necessarily errors.
# INFO - For logging informational messages that highlight the progress of the application.
# DEBUG - For logging detailed information on the flow through the system, primarily used for debugging.
# TRACE - For logging more detailed information than the DEBUG level.
# Set LOGGING_LEVEL to one of the above options to control logging output.
LOGGING_LEVEL=INFO

一定要将USE_DB_AUTHENTICATION设置为false

构建并运行 Docker 容器:

docker compose build
docker compose up -d

这将运行 Firecrawl 的本地实例,可通过 http://localhost:3002 访问。

你应该能够在 http://localhost:3002/admin/@/queues 上看到 管理端Bull Queue Manager UI。

测试

curl -X POST http://localhost:3002/v1/crawl \
    -H 'Content-Type: application/json' \
    -d '{
      "url": "https://docs.firecrawl.dev"
    }'

3.3 其他

开启AI功能
docker-compose.yaml中有对OpenAI的配置

image

因此我们可以跟换为兼容OpenAI API的其他大模型。只需要在.env文件中添加这些设置即可

OPENAI_API_KEY=xxx
OPENAI_BASE_URL=xxx
MODEL_NAME=xxx

然后重启

docker compose down
docker compose up -d 

三、Firecrawl高级配置选项

Firecrawl提供了多种参数来配置抓取过程,以满足不同场景的需求。以下是一些关键配置最佳实践:

(一)抓取选项(Scrape Options)

  1. 内容保存格式

Firecrawl 允许用户选择多种内容保存格式,其中 Markdown 是默认格式,还支持 HTML、Raw HTML(整个网页的简单复制粘贴)、Links(页面中的链接列表)和 Screenshot(页面截图,其值为临时链接,有效期为 24 小时)。例如,以下代码可以以多种格式抓取 Stripe API 文档的前 5 页:


# Crawl the first 5 pages of the stripe API documentation``
stripe_crawl_result = app.crawl_url(
    url="https://docs.stripe.com/api",
    params={
        "limit": 5,  # Only scrape the first 5 pages including the base-url
        "scrapeOptions": {
            "formats": ["markdown", "html", "links", "screenshot"]
        }
    }
)
  1. 控制抓取内容范围

通过onlyMainContent参数(默认禁用),可以排除导航、页眉和页脚等内容,仅抓取主要内容。例如:

stripe_crawl_result = app.crawl_url(
    url="https://docs.stripe.com/api",
    params={
        "limit": 5,
        "scrapeOptions": {
            "formats": ["markdown", "html"], 
            "onlyMainContent": True,
        },
    },
)

同时,includeTags和excludeTags参数接受 HTML 标签、类和 ID 的白名单 / 黑名单列表,进一步精确控制抓取的元素。例如:

stripe_crawl_result = app.crawl_url(
    url="https://docs.stripe.com/api",
    params={
        "limit": 5,
        "scrapeOptions": {
            "formats": ["markdown", "html"],
            "includeTags": ["code", "#page-header"],
            "excludeTags": ["h1", "h2", ".main-content"],
        },
    },
)

(二)URL 控制(URL Control)

  1. 指定 URL 模式

includePaths参数可用于指定要包含的特定 URL 路径,excludePaths参数用于排除不需要的内容路径。例如,在抓取 Stripe 文档网站时,可以只抓取/payments/*路径下的页面,并排除/terminal/*和/financial - connections/*部分
2. 处理链接关系

allowBackwardLinks参数用于控制是否允许爬取指向已访问页面的链接,allowExternalLinks参数用于管理外部链接的处理。例如:

# Example of URL control parameters
url_control_result = app.crawl_url(
    url="https://docs.stripe.com/",
    params={
        # Only crawl pages under the /payments path
        "includePaths": ["/payments/*"],
        # Skip the terminal and financial-connections sections
        "excludePaths": ["/terminal/*", "/financial-connections/*"],
        # Allow crawling links that point to already visited pages
        "allowBackwardLinks": False,
        # Don't follow links to external domains
        "allowExternalLinks": False,
        "scrapeOptions": {
            "formats": ["html"]
        }
    }
)

# Print the total number of pages crawled
print(f"Total pages crawled: {url_control_result['total']}")

此外,maxDepth参数可以限制爬取的深度,例如设置为 2 时,将只抓取初始页面及其直接链接的页面。

(三)性能与限制(Performance & Limits)

1、控制抓取范围

limit参数用于设置最大抓取页面数,这对于避免在抓取大型网站时消耗过多资源至关重要。例如,在抓取大型电商网站时,可以设置limit为 1000,以限制抓取的页面数量。
2、确保页面完全加载

对于包含动态内容(如 JavaScript 处理的内容)、iFrames 或大型媒体元素的网站,可以使用waitFor参数设置等待时间,让页面有足够时间加载。同时,timeout参数可以确保在页面加载时间过长时,爬虫能够继续执行而不被卡住。例如:

stripe_crawl_result = app.crawl_url(
  url="https://docs.stripe.com/api",
  params={
      "limit": 5,
      "scrapeOptions": {
          "formats": ["markdown", "html"],
          "waitFor": 1000,  # wait for a second for pages to load
          "timeout": 10000,  # timeout after 10 seconds
      },
  },
)

五、Firecrawl 和 LangChain 集成构建 AI 驱动的 Web 爬虫
首先安装 LangChain 及其相关库:

`pip install langchain langchain_community langchain_anthropic langchain_openai`

导入 FireCrawlLoader 类并对其进行初始化(将 ANTHROPIC_API_KEY 和 OPENAI_API_KEY 作为变量添加到 .env 文件中):

from dotenv import load_dotenv
from langchain_community.document_loaders.firecrawl import FireCrawlLoader

load_dotenv()

loader = FireCrawlLoader(
    url="https://python.langchain.com/docs/integrations/providers/",
    mode="crawl",
    params={"limit": 5, "scrapeOptions": {"onlyMainContent": True}},
)

开始爬取​​​​​​​

`# Start the crawl``docs = loader.load()`

下一步进行切块(LLM在文本切块中的应用与实践)​​​​​​​

from langchain_text_splitters import RecursiveCharacterTextSplitter

# Add text splitting before creating the vector store
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
# Split the documents
split_docs = text_splitter.split_documents(docs)

向量存储(构建非英文RAG(Retrieval-Augmented Generation)系统时,embedding很重要)​​​​​​​

from langchain_chroma import Chroma
from langchain.embeddings import OpenAIEmbeddings
from langchain_community.vectorstores.utils import filter_complex_metadata

# Create embeddings for the documents
embeddings = OpenAIEmbeddings()

# Create a vector store from the loaded documents
docs = filter_complex_metadata(docs)
vector_store = Chroma.from_documents(docs, embeddings)

最后一步是使用 Claude 3.5 Sonnet 作为语言模型构建 QA 链:​​​​​​​

from langchain.chains import RetrievalQA
from langchain_anthropic import ChatAnthropic

# Initialize the language model
llm = ChatAnthropic(model="claude-3-5-sonnet-20240620", streaming=True)
# Create a QA chain
qa_chain = RetrievalQA.from_chain_type(
    llm=llm,
    chain_type="stuff",
    retriever=vector_store.as_retriever(),
)

现在,我们可以询问有关我们文件的问题:​​​​​​​

# Example question
query = "What is the main topic of the website?"
answer = qa_chain.invoke(query)

print(answer)

{
    'query': 'What is the main topic of the website?',
    'result': """The main topic of the website is LangChain's integrations with Hugging Face. 
    The page provides an overview of various LangChain components that can be used with 
    Hugging Face models and services, including:
    1. Chat models
    2. LLMs (Language Models) 
    3. Embedding models
    4. Document loaders
    5. Tools
    The page focuses on showing how to use different Hugging Face functionalities within 
    the LangChain framework, such as embedding models, language models, datasets, and 
    other tools."""
}

六、使用案例

部署好了之后,我们就来详细看下如何使用吧。

6.1 scrape 36单网页爬取

首先我们可以通过REST API的方式向firecrawl发送指令,我们首先可以使用 /scrape 这个API来抓取一下他们自己公司的网站,使用如以下curl命令:




    curl -X POST http://localhost:3002/v1/scrape     -H 'Content-Type: application/json'  -d '{      "url": "https://36kr.com/information/contact/"   }'

在这里插入图片描述

当然也可以通过各种API工具来发送,使用体验会更好一些。
在这里插入图片描述

3.2 crawl 整站爬取

除了单个网页的抓取,firecrawl还可以抓取多个页面,使用/crawl API ,您给出要抓取的基本 URL,所有可访问的子页面都将被抓取。

curl -X POST <https://api.firecrawl.dev/v0/crawl> \\`    `-H 'Content-Type: application/json' \\`    `-d '{`      `"url": "<https://mendable.ai>",`      `"pageOptions": {`        `"onlyMainContent": true,`        `"includeHtml": true,`        `"screenshot": true,`        `"waitFor": 5000`      `},`      `"crawlerOptions": {`        `"includes": ["/blog/*", "/products/*"],`        `"maxDepth": 3,`        `"mode": "fast",`      `}`    `}'

在此示例中,爬虫将完成以下功能:

  • 仅抓取与模式/blog/*,/products/*和匹配的 URL。
  • 跳过与模式/admin/* ,/login/*和匹配的 URL。
  • 返回每页的完整文档数据。
  • 爬行至最大深度 3。
  • 使用快速抓取模式。
  • 最多抓取 1000 个页面。

/crawl 不会直接返回结果,发送命令后会返回对应任务的JobID,之后的任务是会在后台执行,可以在前面展示过的管理后台里查看任务的运行情况。

image

关于API的所有参数选项,可以到文档中查看。

image

结合大模型

首先 firecrawl 支持对抓取的内容进行大模型提取,利用大模型的能力,直接将抓取结果按预期进行格式化处理。如果想使用这个能力,首先要在.env里配置对应的API KEY。

image

我们在抓取的命令里,加入extractorOptions,参考如下:

"extractorOptions": {`        `"mode": "llm-extraction",`        `"extractionPrompt": "Based on the information on the page, extract the information from the schema. ",`        `"extractionSchema": {`          `"type": "object",`          `"properties": {`            `"company_mission": {`                      `"type": "string"`            `},`            `"supports_sso": {`                      `"type": "boolean"`            `},`            `"is_open_source": {`                      `"type": "boolean"`            `},`            `"is_in_yc": {`                      `"type": "boolean"`            `}`          `},`          `"required": [`            `"company_mission",`            `"supports_sso",`            `"is_open_source",`            `"is_in_yc"`          `]`        `}`      `}

这样再次执行后,可以看到结果的最后就会有提取的内容。

image

通过代码使用

除了这种方式以外,还可以通过代码的方式来使用firecrawl,目前支持

Python SDK
Node SDK
Langchain Integration 🦜🔗
Llama Index Integration 🦙
Langchain JS Integration 🦜🔗

使用代码的方式会有更多的灵活性,可以更加方便的和自己的程序集成起来。下面是一个简单的例子,我用node sdk为例,先要安装:

npm install @mendable/firecrawl-js

然后就可以在代码中使用了

const crawlUrl = ‘<https://example.com>’;const params = {` `crawlerOptions: {` `excludes: ['blog/'],` `includes: [], // leave empty for all pages` `limit: 1000,` `},` `pageOptions: {` `onlyMainContent: true` `}};const waitUntilDone = true;const timeout = 5;const crawlResult = await app.crawlUrl(` `crawlUrl,` `params,` `waitUntilDone,` `timeout);

这里需要说的,默认情况下,官方的SDK中使用的是https://api.firecrawl.dev/ 的在线服务,如果你觉得没问题,那需要去网站上申请API key。

如果你想要使用自己本地启动的服务,那需要自己去修改一下SDK的实现,相关的代码在项目中都开了,大概在这里。

在这里插入图片描述

自己把相关内容修改一下,然后自己使用就可以了。

举个栗子

另外还有一些比较不错的玩法,比如使用 firecrawl 的 /search API来信息收集,之后快速转换成LLM-ready的数据。

首先 search的内容大概是这样的。

import FirecrawlApp from '@mendable/firecrawl-js';``   ``//这里的API key根据自己需要了``const app = new FirecrawlApp({ apiKey: 'YOUR_API_KEY' });``const params = {`    `searchOptions: {`      `limit: 5,`    `},`    `pageOptions: {`        `fetchPageContent: false`    `}`  `};``   ``// Perform a search``const result = await app.search('What is firecrawl?');

会拿到类似这样一个结果。

在这里插入图片描述

剩下就不一步步实现了接下来就简单说下思路:

我们可以针对返回的结果,再对每一个URL使用 /scrape
配置参数用上之前的llm-extraction,就可以对每一个结果抓取数据并提炼内容
再把提炼的结果存下来,比如存到存到向量数据库中
接下来这就可以作为LLM的辅助信息来使用了

总结

整体来说firecrawl的可玩性确实非常高,在AI和大模型的时代,给爬虫这个传统工具提供了一个非常有意思的新发展思路。如果你也感兴趣,可以自己动手来玩一玩,还是很有意思的。

<think>好的,用户遇到了Dify在使用Firecrawl同步知识库时爬行速度过慢的问题,特别是在Docker环境中。我需要根据提供的引用信息来思考解决方案。首先,用户提到引用了几个关于Docker配置的文档,比如启动命令、环境变量设置、Redis地址配置以及Docker Compose版本的问题。我需要结合这些信息来找出可能影响爬虫速度的因素。 首先,用户可能没有正确配置环境变量,尤其是Redis的地址。如果Firecrawl和Dify不在同一个Docker网络内,使用localhost可能无法正确连接,导致延迟。根据引用[2],如果使用Docker,需要调整Redis地址,比如使用Docker的内部网络IP或者服务名。这可能需要检查docker-compose.yaml中的网络配置,确保所有相关服务在同一个自定义网络中,这样可以通过服务名进行通信,减少网络延迟。 其次,Docker的资源配置可能不足。比如,CPU和内存限制可能导致Firecrawl运行缓慢。用户需要检查docker-compose.yaml中的资源限制部分,适当增加CPU和内存的分配。此外,调整ulimits参数,如打开文件数限制,可能会提升并发处理能力。引用[3]提到群晖DSM的Docker Compose版本较低,需要手动修改ulimits设置,这可能也适用于其他环境。需要确保ulimits设置足够高,避免因资源限制导致的性能瓶颈。 另外,Firecrawl本身的配置参数可能需要优化。比如并发请求数、请求间隔时间等。用户可能需要在环境变量中调整这些参数,比如增加并发数,减少延迟时间。同时,检查是否启用了缓存功能,合理设置缓存时间,避免重复抓取相同内容,节省时间和资源。 网络方面,如果Docker容器之间的通信存在延迟,可能需要优化网络配置,使用自定义网络,或者确保容器部署在同一主机上,减少网络开销。此外,检查是否配置了正确的DNS解析,避免DNS查询带来的延迟。 还有,更新Dify和Firecrawl到最新版本可能包含性能改进。用户需要确保从正确的仓库克隆源码,如引用[4]提到的gitee仓库,并定期拉取更新。同时,检查日志文件,查看是否有错误或警告信息,帮助定位问题根源。 最后,如果问题依然存在,可能需要进一步分析,比如使用性能监控工具查看容器资源使用情况,或者联系Firecrawl的开发者社区寻求帮助。综合以上步骤,应该能够有效提升爬虫速度。</think>### 分步解决方案:优化Docker环境中Dify使用Firecrawl同步知识库的爬虫速度 #### 1. 检查Redis地址配置(关键优化) - **问题分析**:Firecrawl依赖Redis进行任务队列管理,若地址配置错误会导致通信延迟[^2] - **操作步骤**: 1. 修改`docker-compose.yaml`中Redis连接配置: ```yaml environment: REDIS_URL: "redis://redis:6379/0" # 使用Docker服务名替代localhost ``` 2. 确保Redis服务在Docker网络内可访问 3. 验证连接:进入Dify容器执行`redis-cli -h redis ping` #### 2. 调整Docker资源配置 - **问题分析**:容器默认资源限制可能成为性能瓶颈 - **优化方案**: ```yaml services: firecrawl: deploy: resources: limits: cpus: '2' # 分配2个CPU核心 memory: 2G # 分配2GB内存 ulimits: # 参考引用[3]调整文件限制 nofile: soft: 65535 hard: 65535 ``` #### 3. 优化Firecrawl配置参数 - **核心参数调整**: ```yaml environment: MAX_CONCURRENT_REQUESTS: "10" # 提升并发请求数 REQUEST_DELAY_MS: "500" # 适当降低请求间隔 CACHE_ENABLED: "true" # 启用缓存 CACHE_TTL: "86400" # 设置24小时缓存有效期 ``` #### 4. Docker网络优化 - **自定义网络配置**: ```yaml networks: dify-net: driver: bridge attachable: true ``` - 确保所有相关服务(Dify/Firecrawl/Redis)使用同一网络 #### 5. 版本升级与日志分析 - **执行步骤**: 1. 更新到最新版本(参考引用[4]): ```bash cd dify && git pull origin main docker-compose build --no-cache && docker-compose up -d ``` 2. 查看实时日志: ```bash docker logs -f firecrawl # 观察请求响应时间 docker stats # 监控资源利用率 ``` #### 6. 高级优化方案(可选) - 启用HTTP/2协议支持: ```yaml environment: HTTP2_ENABLED: "true" ``` - 配置DNS缓存: ```yaml dns: 8.8.8.8 dns_search: . ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赤胜骄阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值