跳到主要内容
SitemapLoader 继承自 WebBaseLoader,它从给定的 URL 加载站点地图,然后抓取并加载站点地图中的所有页面,将每个页面作为文档返回。 抓取是并发进行的。并发请求有合理的限制,默认为每秒 2 个。如果你不关心“好公民”原则,或者你控制被抓取的服务器,或者不关心负载,你可以增加此限制。请注意,虽然这会加速抓取过程,但可能会导致服务器阻止你。请小心!

概览

集成详情

类别本地可序列化JS 支持
SiteMapLoaderlangchain-community

加载器功能

来源文档延迟加载原生异步支持
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
sitemap_loader = SitemapLoader(web_path="https://python-api.langchain.ac.cn/sitemap.xml")

加载

docs = sitemap_loader.load()
docs[0]
Fetching pages: 100%|##########| 28/28 [00:04<00:00,  6.83it/s]
Document(metadata={'source': 'https://python-api.langchain.ac.cn/en/stable/', 'loc': 'https://python-api.langchain.ac.cn/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://python-api.langchain.ac.cn/en/stable/', 'loc': 'https://python-api.langchain.ac.cn/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。通常你不需要其中的每一个。你可以通过将字符串列表或正则表达式模式传递给 filter_urls 参数来过滤 URL。只有与其中一个模式匹配的 URL 才会被加载。
loader = SitemapLoader(
    web_path="https://python-api.langchain.ac.cn/sitemap.xml",
    filter_urls=["https://python-api.langchain.ac.cn/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://python-api.langchain.ac.cn/en/latest/', 'loc': 'https://python-api.langchain.ac.cn/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://python-api.langchain.ac.cn/sitemap.xml",
    filter_urls=["https://python-api.langchain.ac.cn/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 参考:python.langchain.com/api_reference/community/document_loaders/langchain_community.document_loaders.sitemap.SitemapLoader.html#langchain_community.document_loaders.sitemap.SitemapLoader
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.