Skip to main content
Extends from the WebBaseLoader, SitemapLoader loads a sitemap from a given URL, and then scrapes and loads all pages in the sitemap, returning each page as a Document. The scraping is done concurrently. There are reasonable limits to concurrent requests, defaulting to 2 per second. If you aren’t concerned about being a good citizen, or you control the scrapped server, or don’t care about load you can increase this limit. Note, while this will speed up the scraping process, it may cause the server to block you. Be careful!

Overview

Integration details

Loader features

Setup

To access SiteMap document loader you’ll need to install the langchain-community integration package.

Credentials

No credentials are needed to run this. To enable automated tracing of your model calls, set your LangSmith API key:

Installation

Install langchain-community.

Fix notebook asyncio bug

Initialization

Now we can instantiate our model object and load documents:

Load

You can change the requests_per_second parameter to increase the max concurrent requests. and use requests_kwargs to pass kwargs when send requests.

Lazy load

You can also load the pages lazily in order to minimize the memory load.

Filtering sitemap URLs

Sitemaps can be massive files, with thousands of URLs. Often you don’t need every single one of them. You can filter the URLs by passing a list of strings or regex patterns to the filter_urls parameter. Only URLs that match one of the patterns will be loaded.

Add custom scraping rules

The SitemapLoader uses beautifulsoup4 for the scraping process, and it scrapes every element on the page by default. The SitemapLoader constructor accepts a custom scraping function. This feature can be helpful to tailor the scraping process to your specific needs; for example, you might want to avoid scraping headers or navigation elements. The following example shows how to develop and use a custom function to avoid navigation and header elements. Import the beautifulsoup4 library and define the custom function.
Add your custom function to the SitemapLoader object.

Local sitemap

The sitemap loader can also be used to load local files.

API reference

For detailed documentation of all SiteMapLoader features and configurations head to the API reference: python.langchain.com/api_reference/community/document_loaders/langchain_community.document_loaders.sitemap.SitemapLoader.html#langchain_community.document_loaders.sitemap.SitemapLoader