0%

Python实现备份CSDN博客(未完成)

想着把我CSDN上所有博客的markdown源文件备份一遍,但其实没学过这方面的东西,就做了个半成品。。。以后完善吧(应该)

实现了提取所有文章的名字与其对应文章ID和发布时间,并找到编辑文档的网页。 1

上面这些信息提取很方便,网页源码里翻就找得到。 然后按照文件名与日期给每一篇文章建立每一个空文件夹。 2

但是最关键的一步是,我不知道怎么提取每一篇.md文件。。。 渲染好的已经发布的文本我不要,我要.md源文件。只有编辑文本的时候遇得到md文件,所以就得实现模拟浏览器登陆。。。然而并不会做。。。 http://write.blog.csdn.net/postedit/xxxxxxxx是文章的编辑页面,(打开来可以是markdown编辑器也可以是传统的那个),传统的编辑器的源码里倒是可以看到带样式的每个字,但是md编辑器的源码里面就没有。。。

md编辑器有个功能叫导出到本地,可以直接把.md文件下载下来。但是每次下载生成的链接都是随机的,我也不知道怎么找到这个函数。

有没有大神会的求留言:P。

半成品代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#coding=utf-8
'''
Created on 2017年8月19日

@author: XieNaoban
@github: https://github.com/xienaoban
@blog: http://blog.csdn.net/xienaoban
'''

import os
import urllib

#########################################################################
def log(str):
print("log: " + str + ".")

#########################################################################
#参数设定
auth = "xienaoban"
wsp = "C:\\Users\\xjf19\\Desktop\\"

#########################################################################
s = urllib.request.urlopen("http://blog.csdn.net/" + auth).read().decode('utf-8')
pages = ord(s[s.find("尾页") - 3]) - ord('0') + 1
fout = open(wsp + "articles.txt",'w', encoding='utf-8')
log(str(pages) + " page(s) found")

key = "<span class=\"link_title\"><a href=\"/" + auth + "/article/details/"
key2 = "link_postdate"
edit = "http://write.blog.csdn.net/postedit/"
articles = 0
for page in range(1, pages):
url = "http://blog.csdn.net/" + auth + "/article/list/"+str(page)
sc = urllib.request.urlopen(url).read().decode('utf-8')
i = 0
while True:
tmp = sc.find(key, i)
if(tmp < i): break
i = tmp + len(key)
fout.write(edit)
while sc[i]!='\"':
fout.write(sc[i])
i += 1
i += 12

d = sc.find(key2, i) + len(key2) + 2
fout.write(" " + sc[d]+sc[d+1]+sc[d+2]+sc[d+3] + sc[d+5]+sc[d+6] + sc[d+8]+sc[d+9])

ed = sc.find("</a>",i) - 22
while i != ed:
if sc[i] !='/' and sc[i] != '*' and sc[i] != '?': fout.write(sc[i])
i += 1
fout.write("\n")
articles += 1
fout.close()
log(str(articles) + " article(s) found")

fin = open(wsp + "articles.txt", "r", encoding='utf-8')
wsp += "Blog\\"
if not os.path.exists(wsp): os.mkdir(wsp)
new_dir = 0
for arti in range(0, articles):
line = fin.readline()
if not line: break
line = line[len(edit) + 4 + 8:-1]
if not os.path.exists(wsp + line):
os.mkdir(wsp + line)
new_dir += 1

log(str(new_dir) + " directory(s) added")
log("program finished")
#########################################################################