CSDN博客导出为Hexo的MD文件
CSDN博客导出为Hexo的MD文件
一.获取所有文章地址、 二.使用脚本进行采集
部分代码修正:
if __name__ == '__main__':
url_list = [{'url': item['url'], 'title': item['title']} for item in data]
# 解析地址
base_url = 'https://www.helloworld.net/getUrlHtml?url='
# 解析错误的url
err_list = []
for item in url_list:
try:
print(item['url'])
res = requests.get(base_url + item['url'])
content = res.json().get('html')
title = item['title']
print(title+'已完成')
# 调用函数,将HTML转换为Markdown并保存为文件
html_to_md(content, os.path.join('md', f'{title}.md'))
except Exception as e:
print(e)
err_list.append(item['url'])
if err_list:
print(err_list)
df = pd.DataFrame([{'name': err_list}])
df.to_csv('err.csv', index=False)
注:可能出现的问题:博客名不合规无法保存——修改文件名
三.批量给md文件增加hexo识别码
hexo识别文章需要添加如下信息:
---
title: "XXX"
date: 20XX-XX-XX XX:XX:XX
tags: ['AA', 'BB',...]
......(依据manu设置)
---
对于CSDN批量导出文章,采用以下方法快速增加相关信息:
- 创建 CSDN_INFO.txt 保存之前在浏览器console导出的文章信息
- 使用脚本 AddInfo_CSDN.py 将信息自动添加到对应md文件中,将需要转换的 .md 文件保存在同目录的 md 文件夹中
AddInfo_CSDN.py 内容如下:
# 为所有md文件开头添加对应CSDN_INFO.txt中的信息
# ---
# title: "标题"
# date: 2020-01-01 00:00:00
# tags: ["标签1", "标签2"...]
# ---
import os
import re
import time
# 读取CSDN_INFO.txt,提取每篇文章对应的文章标题title、日期postTime、标签tags
def readCSDNInfo():
CSDNInfo = {}
with open("CSDN_INFO.txt", "r", encoding="utf-8") as f:
# 字符匹配title、postTime、tags
title = re.findall(r"title\": \"(.*?)\"", f.read())
f.seek(0)#文件指针重置
postTime = re.findall(r"postTime\": \"(.*?)\"", f.read())
f.seek(0)
all_tags = re.findall(r'"tags":\s*\[([^\]]+)\]', f.read())
tags = [None] * len(title)
# 进一步提取每个标签
for tag_group in all_tags:
# 提取双引号中的内容
tags[all_tags.index(tag_group)] = re.findall(r'"([^"]+)"', tag_group)
# 提取title、postTime、tags
for i in range(len(title)):
CSDNInfo[title[i]] = {}
CSDNInfo[title[i]]["postTime"] = postTime[i]
CSDNInfo[title[i]]["tags"] = tags[i]
return CSDNInfo
# 为md文件开头添加对应CSDN_INFO.txt中的信息
def addInfo2MD(mdPath, CSDNInfo):
title = os.path.basename(mdPath).split(".")[0]
if title in CSDNInfo:
with open(mdPath, "r", encoding="utf-8") as f:
lines = f.readlines()
if lines[0].startswith("---"):
return
with open(mdPath, "r", encoding="utf-8") as f:
content = f.read()
with open(mdPath, "w", encoding="utf-8") as f:
f.write("---\n")
f.write(f"title: \"{title}\"\n")
f.write(f"date: {CSDNInfo[title]['postTime']}\n")
f.write(f"tags: {CSDNInfo[title]['tags']}\n")
f.write("---\n")
f.write(content)
print(f"AddInfo: {title}")
else:
print(f"Error: {title} not in CSDN_INFO.txt")
# 遍历所有md文件
def traverseMDFiles(mdDir, CSDNInfo):
for root, dirs, files in os.walk(mdDir):
for file in files:
if file.endswith(".md"):
mdPath = os.path.join(root, file)
addInfo2MD(mdPath, CSDNInfo)
if __name__ == "__main__":
mdDir = "md"
CSDNInfo = readCSDNInfo()
traverseMDFiles(mdDir, CSDNInfo)
print("Done!")
posted on 2025-02-08 17:51 switch_swq 阅读(2) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)