一、crawl4ai 框架簡介
1. 框架定位
-
核心功能:基于Python的智能爬蟲框架,集成AI(如NLP/OCR)實現自動化數據采集與處理
-
關鍵特性:
-
零配置快速啟動(自動識別網頁結構)
-
內置反反爬機制(自動輪換UA/IP)
-
AI輔助解析(處理驗證碼/動態內容)
-
2. 技術棧組成
3.了解更多請點擊github官方地址
二、環境準備
1. 安裝框架
# 安裝核心庫(需Python≥3.8)
pip install crawl4ai# 可選:安裝AI擴展包
pip install "crawl4ai[ai]" # 包含OCR/NLP依賴
2. 驗證安裝?
import craw14ai
print(craw14ai.__version__) # 應輸出類似 0.2.1
三、實戰項目:智能新聞采集系統
目標:自動抓取新聞網站標題/正文/發布時間,并提取關鍵詞
步驟1:創建基礎爬蟲
from crawl4ai import SmartSpider# 初始化爬蟲(自動加載默認配置)
spider = SmartSpider(name="news_crawler",ai_support=True # 啟用AI輔助
)# 添加種子URL(示例:BBC新聞科技版)
spider.add_seeds(["https://www.bbc.com/news/technology"])
步驟2:定義抓取規則(AI自動學習模式)
# 啟用智能模式自動分析頁面結構
spider.learn(target_elements=["title", "article", "publish_time"],sample_url="https://www.bbc.com/news/technology-12345678" # 提供樣例頁面
)
步驟3:運行并保存數據
# 啟動爬蟲(限制10頁)
results = spider.crawl(max_pages=10)# 保存為JSON文件
import json
with open('news.json', 'w') as f:json.dump(results, f, indent=2)
步驟4:AI增強處理
# 提取新聞關鍵詞(需安裝AI擴展)
from crawl4ai.ai import NLPProcessornlp = NLPProcessor()
for news in results:news['keywords'] = nlp.extract_keywords(news['article'])print(f"標題:{news['title']}\n關鍵詞:{news['keywords'][:3]}\n")
四、進階功能示例
1. 處理驗證碼
spider = SmartSpider(anti_captcha=True, # 自動調用內置OCRcaptcha_config={'type': 'image', # 支持reCAPTCHA/hCaptcha'timeout': 15 # 超時設置}
)
2. 動態渲染頁面
spider.render(engine='playwright', # 可選seleniumwait_for=".article-content", # 等待元素加載screenshot=True # 截圖存檔
)
3. 數據清洗管道
# 自定義處理鉤子
def clean_date(raw_date):from datetime import datetimereturn datetime.strptime(raw_date, "%d %B %Y").isoformat()spider.add_pipeline(field="publish_time",processor=clean_date
)
五、調試技巧
-
日志查看:
spider.set_log_level('DEBUG') # 顯示詳細請求過程
-
保存中間結果:
spider.enable_cache('cache_dir') # 斷點續爬
-
性能監控:
watch -n 1 "ls -lh data.json" # 實時查看數據增長
六、項目結構建議
/news_crawler
├── config/ # 配置文件
│ └── proxies.txt # 代理IP列表
├── outputs/ # 數據輸出
├── spiders/ # 爬蟲邏輯
│ └── bbc_news.py
└── requirements.txt
常見問題解決
-
被封IP:
啟用代理池?spider.set_proxies(file='config/proxies.txt')
-
元素定位失敗:
使用AI輔助定位?spider.find_ai(element_description='新聞正文')
-
動態加載內容:
開啟渲染?spider.render(engine='playwright')