『python爬虫』使用docling 将pdf或html网页转为MD (保姆级图文)

目录


欢迎关注 『python爬虫』 专栏,持续更新中
欢迎关注 『python爬虫』 专栏,持续更新中

预览效果

支持转化pdf的表格

安装

Docling 本身是专注于文档转换的工具,通常用于将文件(如 PDF)转换为其他格式(如 Markdown)

要求python3.11版本+

javascript 复制代码
pip install docling

下载模型

  • 第一次运行需要下载外网的模型,需要tz保证网络通畅,否则会一直飘红报错
  • 可能需要较长时间计算分析,尤其是你没有GPU 使用CPU的情况
javascript 复制代码
Fetching 9 files: 100%|██████████| 9/9 [00:00<00:00, 15840.85it/s]
Neither CUDA nor MPS are available - defaulting to CPU. Note: This module is much faster with a GPU.

测试代码

javascript 复制代码
from docling.document_converter import DocumentConverter

# 第一次运行需要下载外网的模型,需要tz保证网络通畅

# 外网pdf论文
# source = "https://arxiv.org/pdf/2206.01062"  # document per local path or URL

# 可以是网页
# source = "https://blog.csdn.net/u011027547/article/details/143885170"  # document per local path or URL

# 可以是pdf
source = "https://www.gov.cn/zhengce/zhengceku/2022-11/12/5726417/files/b0d97070c0674ab0a1dec4ccd45dd726.pdf"  # document per local path or URL

# 初始化 DocumentConverter
converter = DocumentConverter()

# 执行转换
result = converter.convert(source)

# 获取转换后的 Markdown 内容
markdown_content = result.document.export_to_markdown()

# 保存到 .md 文件
with open(source.split('/')[-1]+".md", "w", encoding="utf-8") as md_file:
    md_file.write(markdown_content)

print("Markdown 文件已保存为 "+source.split('/')[-1]+".md")

总结

大家喜欢的话,给个👍,点个关注!给大家分享更多计算机专业学生的求学之路!

版权声明:

发现你走远了@mzh原创作品,转载必须标注原文链接

Copyright 2023 mzh

Crated:2023-3-1

欢迎关注 『python爬虫』 专栏,持续更新中
欢迎关注 『python爬虫』 专栏,持续更新中
『未完待续』


相关推荐
默_笙2 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
qq_426003962 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫2 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技2 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时2 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
奇思妙想聪明勤奋的小羊2 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1232 天前
2026年第38周GitHub趋势周报
python·科技·github
IZero072 天前
Jev 与 Laya
python·语言模型