> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# BSHTMLLoader 集成

> 使用 LangChain Python 与 BSHTMLLoader 文档加载器集成。

本指南提供了快速入门 BeautifulSoup4 [文档加载器](/oss/python/integrations/document_loaders) 的概览。有关所有 BeautifulSoup4 功能和配置的详细文档，请参阅 [API 参考](https://reference.langchain.com/python/langchain-community/document_loaders/html_bs/BSHTMLLoader)。

## 概览

### 集成详情

| 类                                                                                                                  | 包                                                                                   |  本地 | 可序列化 | JS 支持 |
| :----------------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------- | :-: | :--: | :---: |
| [`BSHTMLLoader`](https://reference.langchain.com/python/langchain-community/document_loaders/html_bs/BSHTMLLoader) | [`langchain-community`](https://reference.langchain.com/python/langchain-community) |  ✅  |   ❌  |   ❌   |

### 加载器功能

|       来源       | 文档懒加载 | 原生异步支持 |
| :------------: | :---: | :----: |
| `BSHTMLLoader` |   ✅   |    ❌   |

## 设置

要访问 BSHTMLLoader 文档加载器，您需要安装 `langchain-community` 集成包和 `bs4` Python 包。

### 凭证

使用 `BSHTMLLoader` 类不需要任何凭证。

要启用模型调用的自动跟踪，请设置您的 [LangSmith](/langsmith/home) API 密钥：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"
```

### 安装

安装 **langchain-community** 和 **bs4**。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-community bs4
```

## 初始化

现在我们可以实例化模型对象并加载文档：

* 待办事项：使用相关参数更新模型实例化。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.document_loaders import BSHTMLLoader

loader = BSHTMLLoader(
    file_path="./example_data/fake-content.html",
)
```

## 加载

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
docs = loader.load()
docs[0]
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
Document(metadata={'source': './example_data/fake-content.html', 'title': 'Test Title'}, page_content='\nTest Title\n\n\nMy First Heading\nMy first paragraph.\n\n\n')
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
print(docs[0].metadata)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{'source': './example_data/fake-content.html', 'title': 'Test Title'}
```

## 懒加载

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
page = []
for doc in loader.lazy_load():
    page.append(doc)
    if len(page) >= 10:
        # 执行一些分页操作，例如
        # index.upsert(page)

        page = []
page[0]
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
Document(metadata={'source': './example_data/fake-content.html', 'title': 'Test Title'}, page_content='\nTest Title\n\n\nMy First Heading\nMy first paragraph.\n\n\n')
```

## 向 BS4 添加分隔符

我们还可以在调用 soup 的 get\_text 时传递一个分隔符

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
loader = BSHTMLLoader(
    file_path="./example_data/fake-content.html", get_text_separator=", "
)

docs = loader.load()
print(docs[0])
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
page_content='
, Test Title,
,
,
, My First Heading,
, My first paragraph.,
,
,
' metadata={'source': './example_data/fake-content.html', 'title': 'Test Title'}
```

***

## API 参考

有关所有 `BSHTMLLoader` 功能和配置的详细文档，请参阅 [API 参考](https://reference.langchain.com/python/langchain-community/document_loaders/html_bs/BSHTMLLoader)

***

<div className="source-links">
  <Callout icon="terminal-2">
    [连接这些文档](/use-these-docs) 到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/document_loaders/bshtml.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
