用 Firecrawl + Gemini AI 輕鬆爬網兼生成摘要

Ai




利用Firecrawl和Google Gemini進行網頁爬蟲和AI自動摘要的編程實現

隨著網絡內容的快速增長,如何高效提取和總結相關信息成為一大挑戰。在本教程中,我們將演示如何利用Firecrawl進行網頁爬蟲,並使用像Google Gemini這樣的AI模型來處理提取的數據。通過將這些工具整合在Google Colab中,我們創建了一個端到端的工作流程,能夠爬取網頁、檢索有意義的內容,並使用最先進的語言模型生成簡潔的摘要。無論你是想自動化研究、從文章中提取見解,還是構建AI驅動的應用程序,本教程都提供了一個穩健且可適應的解決方案。

安裝必要的庫

首先,我們安裝google-generativeai和firecrawl-py,這兩個庫是本教程所需的基本工具。google-generativeai提供了訪問Google的Gemini API以進行AI驅動的文本生成,而firecrawl-py則通過以結構化格式提取網頁內容來實現網頁爬蟲。

“`python
!pip install google-generativeai firecrawl-py
“`

設置API密鑰

然後,我們在Google Colab中安全地設置Firecrawl API密鑰為環境變量。使用getpass()提示用戶輸入API密鑰,並確保其不被顯示,從而保護其私密性。將密鑰存儲在os.environ中,讓Firecrawl的網頁爬蟲功能在整個會話中無縫進行身份驗證。

“`python
import os
from getpass import getpass

# 輸入你的API密鑰(輸入時將隱藏)
os.environ[“FIRECRAWL_API_KEY”] = getpass(“Enter your Firecrawl API key: “)
“`

初始化Firecrawl

我們通過使用存儲的API密鑰創建一個FirecrawlApp實例來初始化Firecrawl。然後,它爬取指定網頁(在這裡是維基百科的Python編程語言頁面)的內容,並以Markdown格式提取數據。最後,打印爬取內容的長度,讓我們在進一步處理之前驗證成功檢索。

“`python
from firecrawl import FirecrawlApp

firecrawl_app = FirecrawlApp(api_key=os.environ[“FIRECRAWL_API_KEY”])

target_url = “https://en.wikipedia.org/wiki/Python_(programming_language)”
result = firecrawl_app.scrape_url(target_url)
page_content = result.get(“markdown”, “”)
print(“Scraped content length:”, len(page_content))
“`

初始化Google Gemini API

我們使用getpass()安全地捕獲Gemini API密鑰,防止其以明文顯示。genai.configure(api_key=GEMINI_API_KEY)命令設置API客戶端,讓我們能夠無縫地與Google的Gemini AI進行文本生成和摘要任務的交互。這確保了在向AI模型發送請求之前的安全身份驗證。

“`python
import google.generativeai as genai
from getpass import getpass

# 安全地輸入你的Gemini API密鑰
GEMINI_API_KEY = getpass(“Enter your Google Gemini API Key: “)
genai.configure(api_key=GEMINI_API_KEY)
“`

列出可用模型

我們使用genai.list_models()迭代列出Google Gemini API中的可用模型並打印其名稱。這幫助用戶確認其API密鑰可訪問哪些模型,以便選擇適合文本生成或摘要任務的模型。如果找不到模型,此步驟有助於調試和選擇替代方案。

“`python
for model in genai.list_models():
print(model.name)
“`

生成摘要

最後,我們使用genai.GenerativeModel(“gemini-1.5-pro”)初始化Gemini 1.5 Pro模型,並發送請求以生成爬取內容的摘要。它限制輸入文本為4000個字符,以遵循API約束。模型處理請求並返回簡潔的摘要,然後將其打印出來,提供提取的網頁內容的結構化和AI生成的概述。

“`python
model = genai.GenerativeModel(“gemini-1.5-pro”)
response = model.generate_content(f”Summarize this:nn{page_content[:4000]}”)
print(“Summary:n”, response.text)
“`

總結

通過將Firecrawl和Google Gemini結合起來,我們創建了一個自動化的管道,能夠爬取網絡內容並生成有意義的摘要,幾乎不需要額外的努力。本教程展示了多種AI驅動的解決方案,根據API的可用性和配額限制提供靈活性。無論你是在從事NLP應用、研究自動化,還是內容聚合,這種方法都能實現高效的大規模數據提取和摘要。

這個Colab Notebook可以參考。還有,別忘了關注我們的Twitter,加入我們的Telegram頻道和LinkedIn小組。也別忘了加入我們擁有80k+成員的ML SubReddit。

在當前的數字時代,網絡內容的暴增為數據提取和處理帶來了前所未有的挑戰。這篇文章不僅提供了一個實用的技術指導,還反映出AI在簡化數據處理過程中的潛力。隨著AI技術的進步,這種自動化的工作流程將成為未來數據科學領域的標準,讓更多的研究者和開發者能夠專注於更具創意的任務,而不是繁瑣的數據處理。這不僅提升了工作效率,也促進了知識的快速傳播和應用。

以上文章由特價GPT API KEY所翻譯及撰寫。而圖片則由FLUX根據內容自動生成。

🎬 YouTube Premium 家庭 Plan成員一位 只需 HK$148/年

不用提供密碼、不用VPN、無需轉區
直接升級你的香港帳號 ➜ 即享 YouTube + YouTube Music 無廣告播放

立即升級 🔗

🎨 Nano Banana Pro 圖像生成器|打幾句說話就出圖

想畫人像、產品圖、插畫?SSFuture 圖像生成器支援 Flux Gemini Nano Banana Pro 改圖 / 合成, 打廣東話都得,仲可以沿用上一張圖繼續微調。

🆓 Flux 模型即玩,不用登入
🤖 登入後解鎖 Gemini 改圖
📷 支援上載參考圖再生成
⚡ 每天免費額度任你玩
✨ 即刻玩 AI 畫圖
[Enhanced Artistic Prompt]

A highly detailed portrait of a young Asian woman inspired by the delicate, doll-like beauty of Imada Mio (精緻洋娃娃臉). She has large, round, luminous eyes with a deep, expressive gaze, a petite V-shaped face, full rosy cheeks, and porcelain-soft skin. Her expression is a captivating blend of innocence, youthful energy, and subtle flirtation, capturing an aura of pure allure.

Her hair is long, dark chocolate brown, with natural volume and texture—slightly tousled in an artful, “just woke up” messy style (剛睡醒的凌亂感), with soft strands falling over her face and shoulders. She wears an oversized, semi-translucent white button-down boyfriend shirt (男友風白襯衫), loosely draped and unbuttoned at the top, elegantly revealing her collarbones and creating a “bottomless” fashion look (下衣失蹤風格) with a sense of relaxed intimacy.

The overall aesthetic is reminiscent of a contemporary Japanese Gravure photobook (寫真集風格), evoking both pure charm and subtle sensuality (Pure & Sexy vibe). Use bright, high-key natural lighting to create a soft, ethereal glow, with smooth, radiant skin texture. Emphasize pastel color grading and gentle film grain, referencing the signature look of Fujifilm PRO 400H for a dreamy, cinematic finish. Compose the image artistically, focusing on beauty, mood, and expressive detail, as if captured in a candid, intimate moment.

— full body or mid-length portrait, shallow depth of field, delicate highlights, softly blurred minimalist background, inviting eyes, tasteful and elegant composition. 人物:人物姿態表情動作衣著都不變,色調:富士底片日系風格,暖色調,日系輕透感,光影:維持照片中的光影邏輯,輕灑在少女身上,像是日系風格清爽,必須符合原本照片中的光影邏輯,背景:一片海洋與藍天,天空天氣非常晴朗,海與藍天在畫面中維持一半的比例,海的顏色是鮮紅色的海,海非常鮮紅、一片平靜的死海,海上有陽光帶來的一點光班,場景:少女坐在海堤防邊,面對著畫面,而少女的後方是一片遙遙無極的海與藍天,完美的呈現一半的比例,在一個清晨的光線中,陽光並沒有太刺眼,陽光像是輕灑在少女身上還有紅色的海面上,透視關係:構圖不改變,維持原本照片的構圖,平面構圖 A hyper-realistic portrait of a man wearing bright yellow neon glasses, his face illuminated with glowing mathematical formulas and physics equations. Warm golden light reflects from his glasses onto his skin. The man has a serious, intelligent expression, short styled hair, and a trimmed beard. His yellow puffer jacket also glows with floating equations and symbols, giving a futuristic holographic effect. Dark teal bokeh lights in the background create depth and contrast. Ultra-detailed skin texture, cinematic lighting, sharp focus, high-contrast neon glow, sci-fi aesthetic, 8K, volumetric light, depth of field, crisp reflections.