跳至主要內容

Sitemap

SitemapLoader 擴展自 WebBaseLoader,從給定的 URL 載入網站地圖,然後抓取並載入網站地圖中的所有頁面,並將每個頁面作為 Document (文件) 回傳。

抓取是同時進行的。對並行請求有合理的限制,預設為每秒 2 個。如果您不擔心成為一個好公民,或者您控制了抓取的伺服器,或者不在乎負載,您可以增加此限制。請注意,雖然這會加快抓取過程,但可能會導致伺服器阻止您。請小心!

總覽

整合詳細資訊

類別 (Class)套件 (Package)本地 (Local)可序列化 (Serializable)JS 支援
SiteMapLoaderlangchain_community

載入器功能

來源 (Source)文件延遲載入 (Document Lazy Loading)原生非同步支援 (Native Async Support)
SiteMapLoader

設定

要存取 SiteMap 文件載入器,您需要安裝 langchain-community 整合套件。

憑證

執行此程式不需要憑證。

如果您想要獲得模型呼叫的自動最佳追蹤,您也可以透過取消註解以下內容來設定您的 LangSmith API 金鑰

# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
# os.environ["LANGSMITH_TRACING"] = "true"

安裝

安裝 langchain_community

%pip install -qU langchain-community

修復筆記本 asyncio 錯誤

import nest_asyncio

nest_asyncio.apply()

初始化

現在我們可以實例化我們的模型物件並載入文件

from langchain_community.document_loaders.sitemap import SitemapLoader
API 參考:SitemapLoader
sitemap_loader = SitemapLoader(web_path="https://api.python.langchain.com/sitemap.xml")

載入

docs = sitemap_loader.load()
docs[0]
Fetching pages: 100%|##########| 28/28 [00:04<00:00,  6.83it/s]
Document(metadata={'source': 'https://api.python.langchain.com/en/stable/', 'loc': 'https://api.python.langchain.com/en/stable/', 'lastmod': '2024-05-15T00:29:42.163001+00:00', 'changefreq': 'weekly', 'priority': '1'}, page_content='\n\n\n\n\n\n\n\n\n\nLangChain Python API Reference Documentation.\n\n\nYou will be automatically redirected to the new location of this page.\n\n')
print(docs[0].metadata)
{'source': 'https://api.python.langchain.com/en/stable/', 'loc': 'https://api.python.langchain.com/en/stable/', 'lastmod': '2024-05-15T00:29:42.163001+00:00', 'changefreq': 'weekly', 'priority': '1'}

您可以更改 requests_per_second 參數以增加最大並行請求數。並使用 requests_kwargs 在發送請求時傳遞 kwargs。

sitemap_loader.requests_per_second = 2
# Optional: avoid `[SSL: CERTIFICATE_VERIFY_FAILED]` issue
sitemap_loader.requests_kwargs = {"verify": False}

延遲載入

您也可以延遲載入頁面,以最大限度地減少記憶體負載。

page = []
for doc in sitemap_loader.lazy_load():
page.append(doc)
if len(page) >= 10:
# do some paged operation, e.g.
# index.upsert(page)

page = []
Fetching pages: 100%|##########| 28/28 [00:01<00:00, 19.06it/s]

過濾網站地圖 URL

網站地圖可能是包含數千個 URL 的大型檔案。通常您不需要每一個 URL。您可以透過將字串或 regex 模式清單傳遞給 filter_urls 參數來過濾 URL。只有符合其中一個模式的 URL 才會被載入。

loader = SitemapLoader(
web_path="https://api.python.langchain.com/sitemap.xml",
filter_urls=["https://api.python.langchain.com/en/latest"],
)
documents = loader.load()
documents[0]
Document(page_content='\n\n\n\n\n\n\n\n\n\nLangChain Python API Reference Documentation.\n\n\nYou will be automatically redirected to the new location of this page.\n\n', metadata={'source': 'https://api.python.langchain.com/en/latest/', 'loc': 'https://api.python.langchain.com/en/latest/', 'lastmod': '2024-02-12T05:26:10.971077+00:00', 'changefreq': 'daily', 'priority': '0.9'})

新增自定義抓取規則

SitemapLoader 使用 beautifulsoup4 進行抓取過程,預設情況下它會抓取頁面上的每個元素。 SitemapLoader 建構函式接受自定義抓取函數。此功能有助於根據您的特定需求客製化抓取過程;例如,您可能想要避免抓取標頭或導航元素。

以下範例示範如何開發和使用自定義函數來避免導航和標頭元素。

匯入 beautifulsoup4 函式庫並定義自定義函數。

pip install beautifulsoup4
from bs4 import BeautifulSoup


def remove_nav_and_header_elements(content: BeautifulSoup) -> str:
# Find all 'nav' and 'header' elements in the BeautifulSoup object
nav_elements = content.find_all("nav")
header_elements = content.find_all("header")

# Remove each 'nav' and 'header' element from the BeautifulSoup object
for element in nav_elements + header_elements:
element.decompose()

return str(content.get_text())

將您的自定義函數新增到 SitemapLoader 物件。

loader = SitemapLoader(
"https://api.python.langchain.com/sitemap.xml",
filter_urls=["https://api.python.langchain.com/en/latest/"],
parsing_function=remove_nav_and_header_elements,
)

本地網站地圖

網站地圖載入器也可用於載入本機檔案。

sitemap_loader = SitemapLoader(web_path="example_data/sitemap.xml", is_local=True)

docs = sitemap_loader.load()

API 參考

有關所有 SiteMapLoader 功能和配置的詳細文件,請前往 API 參考: https://langchain-python.dev.org.tw/api_reference/community/document_loaders/langchain_community.document_loaders.sitemap.SitemapLoader.html#langchain_community.document_loaders.sitemap.SitemapLoader


此頁面有幫助嗎?