如何加载 HTML
超文本标记语言或HTML是专为在Web浏览器中显示而设计的文档的标准标记语言。
本文介绍如何将 HTML 个文档加载到 LangChain 的 Document 对象中,以便在下游使用。
解析 HTML 文件通常需要专用工具。此处我们演示如何通过 Unstructured 和 BeautifulSoup4 进行解析,二者均可通过 pip 安装。请访问集成页面,查找与更多服务的集成,例如 Azure AI Document Intelligence 或 FireCrawl。
使用 Unstructured 加载 HTML
%pip install unstructured
from langchain_community.document_loaders import UnstructuredHTMLLoader
file_path = "../../docs/integrations/document_loaders/example_data/fake-content.html"
loader = UnstructuredHTMLLoader(file_path)
data = loader.load()
print(data)
API 参考:UnstructuredHTMLLoader
[Document(page_content='My First Heading\n\nMy first paragraph.', metadata={'source': '../../docs/integrations/document_loaders/example_data/fake-content.html'})]
使用 BeautifulSoup4 加载 HTML
我们还可以使用 BeautifulSoup4 来通过 BSHTMLLoader 加载 HTML 文档。这将把 HTML 中的文本提取到 page_content,并将页面标题提取到 metadata 中。title。
%pip install bs4
from langchain_community.document_loaders import BSHTMLLoader
loader = BSHTMLLoader(file_path)
data = loader.load()
print(data)
API 参考:BSHTMLLoader
[Document(page_content='\nTest Title\n\n\nMy First Heading\nMy first paragraph.\n\n\n', metadata={'source': '../../docs/integrations/document_loaders/example_data/fake-content.html', 'title': 'Test Title'})]