不详细揭秘博客园备份随笔
以保存我的合集《短文新编》为例。首先在 https://www.cnblogs.com/caijianhong/collections/18069?page={page}
这个页面将网页源代码爬下来,在里面找出所有随笔的 url 和标题。我们都知道形如 https://www.cnblogs.com/caijianhong/p/{blogid}
的随笔,随笔所有者可以在后面加上 .md
获得它的 markdown,但是 1. 没有标题,所以要从合集那一页把标题拿过来,使用神秘 grep 技巧,参见代码,或搜索 grep 正则
与 awk substr
。2. 需要随笔所有者的 cookie,目测只需要 .CNBlogsCookie
与 .Cnblogs.AspNetCore.Cookies
。然后就用 python 的 requests
库爬网页就是了。
必要时呼唤任意 AI 大语言模型帮助你。
#!/bin/env python3
import requests as rq
import subprocess as sp
headers = {
"cookie": ".CNBlogsCookie=一个字符串;.Cnblogs.AspNetCore.Cookies=另一个字符串",
"User-Agent": "看看你的",
}
def getContent(idx, url, title):
res = rq.get("https://www.cnblogs.com/caijianhong/p/" + url + ".md", headers=headers)
res.encoding = "utf-8"
with open(f"{idx}.{title}.md", "w") as file:
print(f"# {title}", file=file)
print(res.text, file=file)
def runcmd(cmd):
return sp.check_output(cmd, shell=True).decode()
cmds = {
"url": """grep -E '<a class="entrylistItemTitle" href="https://www.cnblogs.com/caijianhong/p/' list.txt | grep -oE '[0-9]{8}'""",
"title": """grep -E '<span role="heading" aria-level="2">' list.txt | awk '{print substr($0, 41, length($0)-48)}'""",
}
urls = runcmd(cmds["url"]).strip().split("\n")
titles = runcmd(cmds["title"]).strip().split("\n")
for idx, url, title in zip(range(len(urls)), urls, titles):
getContent(idx, url, title)
本文来自博客园,作者:caijianhong,转载请注明原文链接:https://www.cnblogs.com/caijianhong/p/18315963
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!