WebBaseLoader#
- class langchain_community.document_loaders.web_base.WebBaseLoader(web_path: str | Sequence[str] = '', header_template: dict | None = None, verify_ssl: bool = True, proxies: dict | None = None, continue_on_failure: bool = False, autoset_encoding: bool = True, encoding: str | None = None, web_paths: Sequence[str] = (), requests_per_second: int = 2, default_parser: str = 'html.parser', requests_kwargs: Dict[str, Any] | None = None, raise_for_status: bool = False, bs_get_text_kwargs: Dict[str, Any] | None = None, bs_kwargs: Dict[str, Any] | None = None, session: Any = None, *, show_progress: bool = True, trust_env: bool = False)[来源]#
WebBaseLoader 文档加载器集成
- Setup:
安装
langchain_community
。pip install -U langchain_community
- Instantiate:
from langchain_community.document_loaders import WebBaseLoader loader = WebBaseLoader( web_path = "https://www.espn.com/" # header_template = None, # verify_ssl = True, # proxies = None, # continue_on_failure = False, # autoset_encoding = True, # encoding = None, # web_paths = (), # requests_per_second = 2, # default_parser = "html.parser", # requests_kwargs = None, # raise_for_status = False, # bs_get_text_kwargs = None, # bs_kwargs = None, # session = None, # show_progress = True, # trust_env = False, )
- Lazy load:
docs = [] for doc in loader.lazy_load(): docs.append(doc) print(docs[0].page_content[:100]) print(docs[0].metadata)
ESPN - Serving Sports Fans. Anytime. Anywhere. {'source': 'https://www.espn.com/', 'title': 'ESPN - Serving Sports Fans. Anytime. Anywhere.', 'description': 'Visit ESPN for live scores, highlights and sports news. Stream exclusive games on ESPN+ and play fantasy sports.', 'language': 'en'}
- Async load:
docs = [] async for doc in loader.alazy_load(): docs.append(doc) print(docs[0].page_content[:100]) print(docs[0].metadata)
ESPN - Serving Sports Fans. Anytime. Anywhere. {'source': 'https://www.espn.com/', 'title': 'ESPN - Serving Sports Fans. Anytime. Anywhere.', 'description': 'Visit ESPN for live scores, highlights and sports news. Stream exclusive games on ESPN+ and play fantasy sports.', 'language': 'en'}
在版本0.3.14中更改:弃用了
aload
(它不是异步的)并实现了一个原生的异步alazy_load
。展开以下内容以获取更多详细信息。How to update
aload
你可以使用
load
进行同步加载,或者使用alazy_load
进行异步懒加载,而不是使用aload
。使用
load
的示例(同步):docs: List[Document] = loader.load()
使用
alazy_load
的示例(异步):docs: List[Document] = [] async for doc in loader.alazy_load(): docs.append(doc)
这是为了将来适应异步的
aload
做准备:docs: List[Document] = await loader.aload()
初始化加载器。
- Parameters:
web_paths (Sequence[str]) – 要加载的Web路径。
requests_per_second (int) – 最大并发请求数。
default_parser (str) – 用于BeautifulSoup的默认解析器。
requests_kwargs (Dict[str, Any] | None) – 用于请求的kwargs
raise_for_status (bool) – 如果HTTP状态码表示错误,则引发异常。
bs_get_text_kwargs (Dict[str, Any] | None) – 用于beautifulsoup4 get_text的关键字参数
bs_kwargs (Dict[str, Any] | None) – 用于beautifulsoup4网页解析的kwargs
show_progress (bool) – 加载页面时显示进度条。
trust_env (bool) – 如果使用代理进行网络请求,例如使用http(s)_proxy环境变量,则设置为True。默认为False。
web_path (str | Sequence[str])
header_template (dict | None)
verify_ssl (bool)
proxies (dict | None)
continue_on_failure (bool)
autoset_encoding (bool)
encoding (str | None)
session (任何)
属性
web_path
方法
__init__
([web_path, header_template, ...])初始化加载器。
异步从web_path中的URL(s)懒加载文本。
aload
()ascrape_all
(urls[, parser])异步获取所有URL,然后返回所有结果的soups。
fetch_all
(urls)使用速率限制并发获取所有URL。
从 web_path 中的 URL 懒加载文本。
load
()将数据加载到Document对象中。
load_and_split
([text_splitter])加载文档并将其分割成块。
scrape
([parser])从网页抓取数据并以BeautifulSoup格式返回。
scrape_all
(urls[, parser])获取所有URL,然后返回所有结果的soups。
- __init__(web_path: str | Sequence[str] = '', header_template: dict | None = None, verify_ssl: bool = True, proxies: dict | None = None, continue_on_failure: bool = False, autoset_encoding: bool = True, encoding: str | None = None, web_paths: Sequence[str] = (), requests_per_second: int = 2, default_parser: str = 'html.parser', requests_kwargs: Dict[str, Any] | None = None, raise_for_status: bool = False, bs_get_text_kwargs: Dict[str, Any] | None = None, bs_kwargs: Dict[str, Any] | None = None, session: Any = None, *, show_progress: bool = True, trust_env: bool = False) None [来源]#
初始化加载器。
- Parameters:
web_paths (Sequence[str]) – 要加载的Web路径。
requests_per_second (int) – 最大并发请求数。
default_parser (str) – 用于BeautifulSoup的默认解析器。
requests_kwargs (Dict[str, Any] | None) – 用于请求的kwargs
raise_for_status (bool) – 如果HTTP状态码表示错误,则引发异常。
bs_get_text_kwargs (Dict[str, Any] | None) – 用于beautifulsoup4 get_text的关键字参数
bs_kwargs (Dict[str, Any] | None) – 用于beautifulsoup4网页解析的kwargs
show_progress (bool) – 加载页面时显示进度条。
trust_env (bool) – 如果使用代理进行网络请求,例如使用http(s)_proxy环境变量,则设置为True。默认为False。
web_path (str | Sequence[str])
header_template (dict | None)
verify_ssl (bool)
proxies (dict | None)
continue_on_failure (bool)
autoset_encoding (bool)
encoding (str | None)
session (任何)
- Return type:
无
- async alazy_load() AsyncIterator[Document] [source]#
从web_path中的URL异步懒加载文本。
- Return type:
AsyncIterator[Document]
- aload() List[Document] [source]#
自版本0.3.14起已弃用:请参阅API参考以获取更新的用法:https://python.langchain.com/api_reference/community/document_loaders/langchain_community.document_loaders.web_base.WebBaseLoader.html 在langchain-community==1.0之前不会移除。
从web_path中的URL异步加载文本到Documents中。
- Return type:
列表[文档]
- async ascrape_all(urls: List[str], parser: str | None = None) List[Any] [source]#
异步获取所有URL,然后返回所有结果的soups。
- Parameters:
urls (列表[字符串])
parser (str | None)
- Return type:
列表[任意类型]
- async fetch_all(urls: List[str]) Any [source]#
同时获取所有URL,并进行速率限制。
- Parameters:
urls (列表[字符串])
- Return type:
任何
- load_and_split(text_splitter: TextSplitter | None = None) list[Document] #
加载文档并将其分割成块。块以文档形式返回。
不要重写此方法。它应该被视为已弃用!
- Parameters:
text_splitter (可选[TextSplitter]) – 用于分割文档的TextSplitter实例。 默认为RecursiveCharacterTextSplitter。
- Returns:
文档列表。
- Return type:
列表[Document]
使用WebBaseLoader的示例