Documentation Index
Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
Use this file to discover all available pages before exploring further.
本指南提供了快速入门 BeautifulSoup4 文档加载器 的概述。有关 BeautifulSoup4 所有功能和配置的详细文档,请参阅 API 参考。
集成详情
加载器特性
| 来源 | 文档惰性加载 | 原生异步支持 |
|---|
BSHTMLLoader | ✅ | ❌ |
要使用 BSHTMLLoader 文档加载器,您需要安装 langchain-community 集成包和 bs4 Python 包。
使用 BSHTMLLoader 类无需任何凭证。
要启用模型调用的自动追踪,请设置您的 LangSmith API 密钥:
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("输入您的 LangSmith API 密钥:")
os.environ["LANGSMITH_TRACING"] = "true"
安装 langchain-community 和 bs4。
pip install -qU langchain-community bs4
初始化
现在我们可以实例化模型对象并加载文档:
from langchain_community.document_loaders import BSHTMLLoader
loader = BSHTMLLoader(
file_path="./example_data/fake-content.html",
)
docs = loader.load()
docs[0]
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')
{'source': './example_data/fake-content.html', 'title': 'Test Title'}
惰性加载
page = []
for doc in loader.lazy_load():
page.append(doc)
if len(page) >= 10:
# 执行一些分页操作,例如:
# index.upsert(page)
page = []
page[0]
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 时传递一个分隔符
loader = BSHTMLLoader(
file_path="./example_data/fake-content.html", get_text_separator=", "
)
docs = loader.load()
print(docs[0])
page_content='
, Test Title,
,
,
, My First Heading,
, My first paragraph.,
,
,
' metadata={'source': './example_data/fake-content.html', 'title': 'Test Title'}
API 参考
有关 BSHTMLLoader 所有功能和配置的详细文档,请参阅 API 参考