不详细揭秘博客园备份随笔

以保存我的合集《短文新编》为例。首先在 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)
posted @   caijianhong  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示