|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
"""Loader that fetches a sitemap and loads those URLs."""
|
|
|
|
|
import itertools
|
|
|
|
|
import re
|
|
|
|
|
from typing import Any, Callable, List, Optional
|
|
|
|
|
from typing import Any, Callable, Generator, Iterable, List, Optional
|
|
|
|
|
|
|
|
|
|
from langchain.document_loaders.web_base import WebBaseLoader
|
|
|
|
|
from langchain.schema import Document
|
|
|
|
@ -10,6 +11,12 @@ def _default_parsing_function(content: Any) -> str:
|
|
|
|
|
return str(content.get_text())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _batch_block(iterable: Iterable, size: int) -> Generator[List[dict], None, None]:
|
|
|
|
|
it = iter(iterable)
|
|
|
|
|
while item := list(itertools.islice(it, size)):
|
|
|
|
|
yield item
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SitemapLoader(WebBaseLoader):
|
|
|
|
|
"""Loader that fetches a sitemap and loads those URLs."""
|
|
|
|
|
|
|
|
|
@ -18,6 +25,8 @@ class SitemapLoader(WebBaseLoader):
|
|
|
|
|
web_path: str,
|
|
|
|
|
filter_urls: Optional[List[str]] = None,
|
|
|
|
|
parsing_function: Optional[Callable] = None,
|
|
|
|
|
blocksize: Optional[int] = None,
|
|
|
|
|
blocknum: int = 0,
|
|
|
|
|
):
|
|
|
|
|
"""Initialize with webpage path and optional filter URLs.
|
|
|
|
|
|
|
|
|
@ -26,8 +35,16 @@ class SitemapLoader(WebBaseLoader):
|
|
|
|
|
filter_urls: list of strings or regexes that will be applied to filter the
|
|
|
|
|
urls that are parsed and loaded
|
|
|
|
|
parsing_function: Function to parse bs4.Soup output
|
|
|
|
|
blocksize: number of sitemap locations per block
|
|
|
|
|
blocknum: the number of the block that should be loaded - zero indexed
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
if blocksize is not None and blocksize < 1:
|
|
|
|
|
raise ValueError("Sitemap blocksize should be at least 1")
|
|
|
|
|
|
|
|
|
|
if blocknum < 0:
|
|
|
|
|
raise ValueError("Sitemap blocknum can not be lower then 0")
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
import lxml # noqa:F401
|
|
|
|
|
except ImportError:
|
|
|
|
@ -39,6 +56,8 @@ class SitemapLoader(WebBaseLoader):
|
|
|
|
|
|
|
|
|
|
self.filter_urls = filter_urls
|
|
|
|
|
self.parsing_function = parsing_function or _default_parsing_function
|
|
|
|
|
self.blocksize = blocksize
|
|
|
|
|
self.blocknum = blocknum
|
|
|
|
|
|
|
|
|
|
def parse_sitemap(self, soup: Any) -> List[dict]:
|
|
|
|
|
"""Parse sitemap xml and load into a list of dicts."""
|
|
|
|
@ -76,6 +95,16 @@ class SitemapLoader(WebBaseLoader):
|
|
|
|
|
|
|
|
|
|
els = self.parse_sitemap(soup)
|
|
|
|
|
|
|
|
|
|
if self.blocksize is not None:
|
|
|
|
|
elblocks = list(_batch_block(els, self.blocksize))
|
|
|
|
|
blockcount = len(elblocks)
|
|
|
|
|
if blockcount - 1 < self.blocknum:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"Selected sitemap does not contain enough blocks for given blocknum"
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
els = elblocks[self.blocknum]
|
|
|
|
|
|
|
|
|
|
results = self.scrape_all([el["loc"].strip() for el in els if "loc" in el])
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|