Skip to content

LangChain文档加载

文档加载器

文档加载器

LangChain 中的 Document Loaders(文档加载器) 是用于从各种数据源(如网页、PDF、云存储、社交媒体、生产力工具等)读取和解析原始内容,并将其转换为 LangChain 内部统一的 Document 对象格式的一类组件。这些 Document 对象通常包含两个核心字段:

  • page_content:文档的文本内容(字符串)
  • metadata:与文档相关的元信息(字典,如来源 URL、文件名、创建时间等)

文档加载器目标

  • 统一接口:无论数据来自 PDF、网页、Slack 还是 Google Drive,都通过相同的 .load().lazy_load() 方法获取 List[Document]
  • 结构化解耦:将“数据获取”与“后续处理(如分块、嵌入、检索)”解耦,便于构建可复用的 RAG(Retrieval-Augmented Generation)流水线。
  • 支持流式加载:对于大数据集,可通过 .lazy_load() 实现内存友好的惰性加载。

通用接口

所有文档加载器都实现自 BaseLoader 接口,提供以下方法:

python
from langchain_community.document_loaders import CSVLoader

loader = CSVLoader(file_path="data.csv")
documents = loader.load()               # 一次性加载全部
for doc in loader.lazy_load():          # 惰性逐个加载
    print(doc)

PDF文档加载示例

安装

shell
pip install -U langchain-community pypdf -i https://mirrors.aliyun.com/pypi/simple --trusted-host=mirrors.aliyun.com

示例

python
from langchain_community.document_loaders import PyPDFLoader

file_path = "./data/pdf/长安大学.pdf"
loader = PyPDFLoader(file_path, mode="single")

# 加载文档
docs = loader.load()
print(len(docs))
# Document对象,包含page_content属性和metadata属性
print(type(docs[0]))
print(docs[0])

Markdown文档加载器示例

安装

shell
pip install -U unstructured unstructured[md,docx,pptx,xlsx,pdf] -i https://mirrors.aliyun.com/pypi/simple --trusted-host=mirrors.aliyun.com

示例

python
from langchain_community.document_loaders import UnstructuredMarkdownLoader

file_path = "./data/md/热门专业top20.md"
loader = UnstructuredMarkdownLoader(
    file_path,
    mode="single",
    strategy="fast",
)
docs = loader.load()
print(len(docs))
print(type(docs[0]))
print(docs[0])

常见文档加载器

文件扩展名当前 Loader是否合理理由说明
便携文档类
pdfPyPDFLoader✅ 基本合理PyPDFLoader 轻量但对扫描 PDF/表格支持弱;PyMuPDF 更快更全
epubUnstructuredEPubLoader✅ 合理Unstructured 对 EPUB 解析成熟
md / markdownUnstructuredMarkdownLoader✅ 合理保留标题/列表结构,优于纯文本加载
txtTextLoader✅ 合理轻量高效,无冗余解析
csvCSVLoader✅ 合理支持列名、分隔符,结构化提取最佳
rtfUnstructuredRTFLoader✅ 合理专用于富文本文档
办公套件类
doc / docxUnstructuredWordDocumentLoader✅ 合理支持段落、表格、页眉页脚
xls / xlsxUnstructuredExcelLoader✅ 合理按 sheet 自动分块,保留结构
ppt / pptxUnstructuredPowerPointLoader✅ 合理按幻灯片分块,保留标题内容
结构化数据类
htmlUnstructuredHTMLLoader✅ 合理自动过滤广告/导航等噪声标签
jsonJSONLoader✅ 合理支持 jq 表达式提取字段
pyPythonLoader✅ 合理按函数/类语法分块,RAG 效果佳

参考资料

网页链接

设计

  • 支持用户提交一个 URL(如 https://rag.marsmgn.cn/ )作为“文档”加入知识库;
  • 后台任务队列:抓取网页内容、解析文本、分块、向量化并入库;
  • 数据模型上尽量兼容,避免为 URL 单独建表。
  • 数据模型调整
    • original_filename:URL的值,解释后变更为URL标题
    • file_size_bytes:0,解释后更新
    • mime_type"text/url"
    • doc_type"url"——重要
    • file_hash:URL 的 SHA256 (用于去重)
    • storage_type"url"
    • storage_path:URL的值

处理流程

  • 校验 URL 合法性(协议、域名白名单等);
  • 计算 url_hash = sha256(url),检查是否已存在(避免重复抓取);
  • 创建 DocumentModel 实例;
  • 入库;
  • 发送异步任务(如 Celery / Redis Queue)到解析队列;
  • 返回 document_id 和状态。

网页加载器

WebBaseLoader实战

安装

bash
pip install -U langchain-community beautifulsoup4 -i https://mirrors.aliyun.com/pypi/simple --trusted-host=mirrors.aliyun.com

使用

py
from langchain_community.document_loaders import WebBaseLoader


url = "https://rag.marsmgn.cn/guide/basic-raq-scenario"
loader = WebBaseLoader(url)
docs = loader.load()

print(docs[0].metadata)

# 删除重复的换行
# cleaned_content = docs[0].page_content.replace("\n", "")
# print(cleaned_content)

输出结果示例

bash
{'source': 'https://news.qq.com/rain/a/20260210A06UZA00', 'title': '中芯国际:逆势加码,国产 AI 芯的 “全村希望”?_腾讯新闻', 'description': '中芯国际(0981.HK/688981.SH)北京时间 2026 年 2 月 10 日晚,港股盘后发布 2025 年度第四季度财报(截至 2025 年 12 月),要点如下:1、整体业绩....', 'language': 'zh-CN'}