如何載入 HTML
超文本標記語言或 HTML 是設計用於在網頁瀏覽器中顯示的文件的標準標記語言。
這涵蓋了如何將 HTML
文件載入到 LangChain Document 物件中,以便我們可以在下游使用。
解析 HTML 檔案通常需要專門的工具。在這裡,我們示範透過 Unstructured 和 BeautifulSoup4 進行解析,這些工具可以透過 pip 安裝。前往整合頁面以尋找與其他服務的整合,例如 Azure AI 文件智慧 或 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
中,並將頁面標題作為 title
提取到 metadata
中。
%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'})]