Crawl4AI ช่วยลดความยุ่งยากในการรวบรวมข้อมูลเว็บแบบอะซิงโครนัสและการแยกข้อมูล ทำให้สามารถเข้าถึงได้สำหรับโมเดลภาษาขนาดใหญ่ (LLM) และแอปพลิเคชัน AI -
CrawlResult
srcset
, picture
และรูปภาพที่ตอบสนองfile://
path และ raw HTML ( raw:
) โดยตรงเล่นรอบกับสิ่งนี้
เยี่ยมชมเว็บไซต์เอกสารของเรา
Crawl4AI เสนอตัวเลือกการติดตั้งที่ยืดหยุ่นเพื่อให้เหมาะกับกรณีการใช้งานที่หลากหลาย คุณสามารถติดตั้งเป็นแพ็คเกจ Python หรือใช้ Docker
เลือกตัวเลือกการติดตั้งที่เหมาะกับความต้องการของคุณมากที่สุด:
สำหรับงานรวบรวมข้อมูลเว็บและขูดข้อมูลพื้นฐาน:
pip install crawl4ai
ตามค่าเริ่มต้น การดำเนินการนี้จะติดตั้ง Crawl4AI เวอร์ชันอะซิงโครนัส โดยใช้ Playwright สำหรับการรวบรวมข้อมูลเว็บ
หมายเหตุ: เมื่อคุณติดตั้ง Crawl4AI สคริปต์การตั้งค่าควรติดตั้งและตั้งค่า Playwright โดยอัตโนมัติ อย่างไรก็ตาม หากคุณพบข้อผิดพลาดเกี่ยวกับนักเขียนบทละคร คุณสามารถติดตั้งได้ด้วยตนเองโดยใช้วิธีใดวิธีหนึ่งต่อไปนี้
ผ่านทางบรรทัดคำสั่ง:
playwright install
หากวิธีการข้างต้นไม่ได้ผล ให้ลองใช้คำสั่งเฉพาะเจาะจงกว่านี้:
python -m playwright install chromium
วิธีที่สองนี้ได้รับการพิสูจน์แล้วว่าเชื่อถือได้มากกว่าในบางกรณี
หากคุณต้องการเวอร์ชันซิงโครนัสโดยใช้ Selenium:
pip install crawl4ai[sync]
สำหรับผู้มีส่วนร่วมที่วางแผนจะแก้ไขซอร์สโค้ด:
git clone https://github.com/unclecode/crawl4ai.git
cd crawl4ai
pip install -e .
ปรับใช้อินสแตนซ์ Crawl4AI ของคุณเองได้ด้วยคลิกเดียว:
ข้อมูลจำเพาะที่แนะนำ : RAM ขั้นต่ำ 4GB เลือก "professional-xs" หรือสูงกว่าเมื่อปรับใช้เพื่อการทำงานที่เสถียร
การปรับใช้จะ:
Crawl4AI มีให้เป็นอิมเมจ Docker เพื่อการปรับใช้ที่ง่ายดาย คุณสามารถดึงโดยตรงจาก Docker Hub (แนะนำ) หรือสร้างจากพื้นที่เก็บข้อมูล
# Pull and run from Docker Hub (choose one):
docker pull unclecode/crawl4ai:basic # Basic crawling features
docker pull unclecode/crawl4ai:all # Full installation (ML, LLM support)
docker pull unclecode/crawl4ai:gpu # GPU-enabled version
# Run the container
docker run -p 11235:11235 unclecode/crawl4ai:basic # Replace 'basic' with your chosen version
# In case you want to set platform to arm64
docker run --platform linux/arm64 -p 11235:11235 unclecode/crawl4ai:basic
# In case to allocate more shared memory for the container
docker run --shm-size=2gb -p 11235:11235 unclecode/crawl4ai:basic
# Clone the repository
git clone https://github.com/unclecode/crawl4ai.git
cd crawl4ai
# Build the image
docker build -t crawl4ai:local
--build-arg INSTALL_TYPE=basic # Options: basic, all
.
# In case you want to set platform to arm64
docker build -t crawl4ai:local
--build-arg INSTALL_TYPE=basic # Options: basic, all
--platform linux/arm64
.
# Run your local build
docker run -p 11235:11235 crawl4ai:local
การทดสอบด่วน (ใช้ได้กับทั้งสองตัวเลือก):
import requests
# Submit a crawl job
response = requests . post (
"http://localhost:11235/crawl" ,
json = { "urls" : "https://example.com" , "priority" : 10 }
)
task_id = response . json ()[ "task_id" ]
# Get results
result = requests . get ( f"http://localhost:11235/task/ { task_id } " )
สำหรับการกำหนดค่าขั้นสูง ตัวแปรสภาพแวดล้อม และตัวอย่างการใช้งาน โปรดดูคู่มือการปรับใช้ Docker ของเรา
import asyncio
from crawl4ai import AsyncWebCrawler
async def main ():
async with AsyncWebCrawler ( verbose = True ) as crawler :
result = await crawler . arun ( url = "https://www.nbcnews.com/business" )
print ( result . markdown )
if __name__ == "__main__" :
asyncio . run ( main ())
import asyncio
from crawl4ai import AsyncWebCrawler
async def main ():
async with AsyncWebCrawler ( verbose = True ) as crawler :
js_code = [ "const loadMoreButton = Array.from(document.querySelectorAll('button')).find(button => button.textContent.includes('Load More')); loadMoreButton && loadMoreButton.click();" ]
result = await crawler . arun (
url = "https://www.nbcnews.com/business" ,
js_code = js_code ,
css_selector = ".wide-tease-item__description" ,
bypass_cache = True
)
print ( result . extracted_content )
if __name__ == "__main__" :
asyncio . run ( main ())
import asyncio
from crawl4ai import AsyncWebCrawler
async def main ():
async with AsyncWebCrawler ( verbose = True , proxy = "http://127.0.0.1:7890" ) as crawler :
result = await crawler . arun (
url = "https://www.nbcnews.com/business" ,
bypass_cache = True
)
print ( result . markdown )
if __name__ == "__main__" :
asyncio . run ( main ())
JsonCssExtractionStrategy
ช่วยให้สามารถดึงข้อมูลที่มีโครงสร้างจากหน้าเว็บได้อย่างแม่นยำโดยใช้ตัวเลือก CSS
import asyncio
import json
from crawl4ai import AsyncWebCrawler
from crawl4ai . extraction_strategy import JsonCssExtractionStrategy
async def extract_news_teasers ():
schema = {
"name" : "News Teaser Extractor" ,
"baseSelector" : ".wide-tease-item__wrapper" ,
"fields" : [
{
"name" : "category" ,
"selector" : ".unibrow span[data-testid='unibrow-text']" ,
"type" : "text" ,
},
{
"name" : "headline" ,
"selector" : ".wide-tease-item__headline" ,
"type" : "text" ,
},
{
"name" : "summary" ,
"selector" : ".wide-tease-item__description" ,
"type" : "text" ,
},
{
"name" : "time" ,
"selector" : "[data-testid='wide-tease-date']" ,
"type" : "text" ,
},
{
"name" : "image" ,
"type" : "nested" ,
"selector" : "picture.teasePicture img" ,
"fields" : [
{ "name" : "src" , "type" : "attribute" , "attribute" : "src" },
{ "name" : "alt" , "type" : "attribute" , "attribute" : "alt" },
],
},
{
"name" : "link" ,
"selector" : "a[href]" ,
"type" : "attribute" ,
"attribute" : "href" ,
},
],
}
extraction_strategy = JsonCssExtractionStrategy ( schema , verbose = True )
async with AsyncWebCrawler ( verbose = True ) as crawler :
result = await crawler . arun (
url = "https://www.nbcnews.com/business" ,
extraction_strategy = extraction_strategy ,
bypass_cache = True ,
)
assert result . success , "Failed to crawl the page"
news_teasers = json . loads ( result . extracted_content )
print ( f"Successfully extracted { len ( news_teasers ) } news teasers" )
print ( json . dumps ( news_teasers [ 0 ], indent = 2 ))
if __name__ == "__main__" :
asyncio . run ( extract_news_teasers ())
สำหรับตัวอย่างการใช้งานขั้นสูงเพิ่มเติม โปรดดูส่วนตัวอย่างของเราในเอกสารประกอบ
import os
import asyncio
from crawl4ai import AsyncWebCrawler
from crawl4ai . extraction_strategy import LLMExtractionStrategy
from pydantic import BaseModel , Field
class OpenAIModelFee ( BaseModel ):
model_name : str = Field (..., description = "Name of the OpenAI model." )
input_fee : str = Field (..., description = "Fee for input token for the OpenAI model." )
output_fee : str = Field (..., description = "Fee for output token for the OpenAI model." )
async def main ():
async with AsyncWebCrawler ( verbose = True ) as crawler :
result = await crawler . arun (
url = 'https://openai.com/api/pricing/' ,
word_count_threshold = 1 ,
extraction_strategy = LLMExtractionStrategy (
provider = "openai/gpt-4o" , api_token = os . getenv ( 'OPENAI_API_KEY' ),
schema = OpenAIModelFee . schema (),
extraction_type = "schema" ,
instruction = """From the crawled content, extract all mentioned model names along with their fees for input and output tokens.
Do not miss any models in the entire content. One extracted model JSON format should look like this:
{"model_name": "GPT-4", "input_fee": "US$10.00 / 1M tokens", "output_fee": "US$30.00 / 1M tokens"}."""
),
bypass_cache = True ,
)
print ( result . extracted_content )
if __name__ == "__main__" :
asyncio . run ( main ())
Crawl4AI เป็นเลิศในการจัดการสถานการณ์ที่ซับซ้อน เช่น การรวบรวมข้อมูลหลายหน้าด้วยเนื้อหาแบบไดนามิกที่โหลดผ่าน JavaScript ต่อไปนี้เป็นตัวอย่างของการรวบรวมข้อมูล GitHub ที่คอมมิตในหลายเพจ:
import asyncio
import re
from bs4 import BeautifulSoup
from crawl4ai import AsyncWebCrawler
async def crawl_typescript_commits ():
first_commit = ""
async def on_execution_started ( page ):
nonlocal first_commit
try :
while True :
await page . wait_for_selector ( 'li.Box-sc-g0xbh4-0 h4' )
commit = await page . query_selector ( 'li.Box-sc-g0xbh4-0 h4' )
commit = await commit . evaluate ( '(element) => element.textContent' )
commit = re . sub ( r's+' , '' , commit )
if commit and commit != first_commit :
first_commit = commit
break
await asyncio . sleep ( 0.5 )
except Exception as e :
print ( f"Warning: New content didn't appear after JavaScript execution: { e } " )
async with AsyncWebCrawler ( verbose = True ) as crawler :
crawler . crawler_strategy . set_hook ( 'on_execution_started' , on_execution_started )
url = "https://github.com/microsoft/TypeScript/commits/main"
session_id = "typescript_commits_session"
all_commits = []
js_next_page = """
const button = document.querySelector('a[data-testid="pagination-next-button"]');
if (button) button.click();
"""
for page in range ( 3 ): # Crawl 3 pages
result = await crawler . arun (
url = url ,
session_id = session_id ,
css_selector = "li.Box-sc-g0xbh4-0" ,
js = js_next_page if page > 0 else None ,
bypass_cache = True ,
js_only = page > 0
)
assert result . success , f"Failed to crawl page { page + 1 } "
soup = BeautifulSoup ( result . cleaned_html , 'html.parser' )
commits = soup . select ( "li" )
all_commits . extend ( commits )
print ( f"Page { page + 1 } : Found { len ( commits ) } commits" )
await crawler . crawler_strategy . kill_session ( session_id )
print ( f"Successfully crawled { len ( all_commits ) } commits across 3 pages" )
if __name__ == "__main__" :
asyncio . run ( crawl_typescript_commits ())
ตัวอย่างนี้แสดงให้เห็นถึงความสามารถของ Crawl4AI ในการจัดการสถานการณ์ที่ซับซ้อนซึ่งมีการโหลดเนื้อหาแบบอะซิงโครนัส โดยรวบรวมข้อมูลหลายหน้าของ GitHub ที่คอมมิต เรียกใช้ JavaScript เพื่อโหลดเนื้อหาใหม่ และใช้ hooks แบบกำหนดเองเพื่อให้แน่ใจว่าข้อมูลถูกโหลดก่อนดำเนินการต่อ
สำหรับตัวอย่างการใช้งานขั้นสูงเพิ่มเติม โปรดดูส่วนตัวอย่างของเราในเอกสารประกอบ
Crawl4AI ได้รับการออกแบบโดยเน้นความเร็วเป็นหลัก เป้าหมายของเราคือการให้การตอบสนองที่เร็วที่สุดเท่าที่จะเป็นไปได้ด้วยการดึงข้อมูลคุณภาพสูง โดยลดสิ่งที่เป็นนามธรรมระหว่างข้อมูลและผู้ใช้ให้เหลือน้อยที่สุด
เราได้ทำการเปรียบเทียบความเร็วระหว่าง Crawl4AI และ Firecrawl ซึ่งเป็นบริการแบบชำระเงิน ผลลัพธ์แสดงให้เห็นถึงประสิทธิภาพที่เหนือกว่าของ Crawl4AI:
Firecrawl:
Time taken: 7.02 seconds
Content length: 42074 characters
Images found: 49
Crawl4AI (simple crawl):
Time taken: 1.60 seconds
Content length: 18238 characters
Images found: 49
Crawl4AI (with JavaScript execution):
Time taken: 4.64 seconds
Content length: 40869 characters
Images found: 89
อย่างที่คุณเห็น Crawl4AI มีประสิทธิภาพเหนือกว่า Firecrawl อย่างมาก:
คุณสามารถค้นหาโค้ดการเปรียบเทียบแบบเต็มได้ในพื้นที่เก็บข้อมูลของเราที่ docs/examples/crawl4ai_vs_firecrawl.py
สำหรับเอกสารประกอบโดยละเอียด รวมถึงคำแนะนำในการติดตั้ง คุณสมบัติขั้นสูง และการอ้างอิง API โปรดไปที่เว็บไซต์เอกสารประกอบของเรา
สำหรับข้อมูลโดยละเอียดเกี่ยวกับแผนการพัฒนาและคุณสมบัติที่กำลังจะมาถึง โปรดดูแผนการทำงานของเรา
เรายินดีรับการสนับสนุนจากชุมชนโอเพ่นซอร์ส ตรวจสอบแนวทางการสนับสนุนของเราสำหรับข้อมูลเพิ่มเติม
Crawl4AI ได้รับการเผยแพร่ภายใต้ลิขสิทธิ์ Apache 2.0
หากมีคำถาม ข้อเสนอแนะ หรือคำติชม โปรดติดต่อ:
มีความสุขในการรวบรวมข้อมูล! ?
ภารกิจของเราคือการปลดล็อกศักยภาพที่ยังไม่ได้ใช้ของข้อมูลส่วนบุคคลและข้อมูลองค์กรในยุคดิจิทัล ในโลกปัจจุบัน บุคคลและองค์กรต่างๆ ได้สร้างรอยเท้าทางดิจิทัลอันทรงคุณค่าจำนวนมหาศาล แต่ข้อมูลนี้ส่วนใหญ่ยังไม่ถือเป็นทรัพย์สินที่แท้จริง
โซลูชันโอเพ่นซอร์สของเราช่วยให้นักพัฒนาและผู้สร้างนวัตกรรมสามารถสร้างเครื่องมือสำหรับการดึงและจัดโครงสร้างข้อมูล ซึ่งเป็นการวางรากฐานสำหรับยุคใหม่ของการเป็นเจ้าของข้อมูล ด้วยการแปลงข้อมูลส่วนบุคคลและองค์กรให้เป็นสินทรัพย์ที่มีโครงสร้างและซื้อขายได้ เรากำลังสร้างโอกาสสำหรับบุคคลในการใช้ประโยชน์จากรอยเท้าดิจิทัลของตน และสำหรับองค์กรในการปลดล็อกคุณค่าของความรู้ที่รวบรวมไว้
การทำให้ข้อมูลเป็นประชาธิปไตยนี้ถือเป็นก้าวแรกสู่เศรษฐกิจการใช้ข้อมูลร่วมกัน โดยที่การมีส่วนร่วมอย่างเต็มใจในการแบ่งปันข้อมูลจะขับเคลื่อนความก้าวหน้าของ AI ในขณะเดียวกันก็รับประกันว่าผู้สร้างข้อมูลจะได้รับประโยชน์กลับมา ด้วยแนวทางนี้ เรากำลังสร้างอนาคตที่การพัฒนา AI ขับเคลื่อนด้วยความรู้ที่แท้จริงของมนุษย์ แทนที่จะเป็นทางเลือกสังเคราะห์
หากต้องการทราบรายละเอียดเกี่ยวกับวิสัยทัศน์ โอกาส และเส้นทางข้างหน้าของเรา โปรดดูพันธกิจฉบับเต็มของเรา
หากต้องการทราบรายละเอียดเกี่ยวกับวิสัยทัศน์ ความท้าทาย และแนวทางแก้ไขของเรา โปรดดูพันธกิจฉบับเต็มของเรา