forked from kesalin/PythonSnippet
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathexportCSDNBlogAsMarkdown.py
executable file
·283 lines (224 loc) · 8.39 KB
/
exportCSDNBlogAsMarkdown.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python
#! encoding=utf-8
# Author : kesalin@gmail.com
# Blog : http://luozhaohui.github.io
# Date : 2014/10/18
# Description : Export CSND blog articles to Markdown files.
# Version : 1.0.0.0
# Python Version: Python 2.7.3
#
import re
import os
import sys
import datetime
import time
import traceback
import codecs
import urllib2
from bs4 import BeautifulSoup
# 获取 url 内容
gUseCookie = False
gHeaders = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36',
'Cookie': 'Put your cookie here'
}
def getHtml(url):
try:
if gUseCookie:
opener = urllib2.build_opener()
for k, v in gHeaders.items():
opener.addheaders.append((k, v))
response = opener.open(url)
data = response.read().decode('utf-8')
else:
request = urllib2.Request(url, None, gHeaders)
response = urllib2.urlopen(request)
data = response.read().decode('utf-8')
except urllib2.URLError as e:
if hasattr(e, "code"):
print("The server couldn't fulfill the request: " + url)
print("Error code: %s" % e.code)
elif hasattr(e, "reason"):
print("We failed to reach a server. Please check your url: " +
url + ", and read the Reason.")
print("Reason: %s" % e.reason)
return data
def slow_down():
time.sleep(0.5) # slow down a little
def log(str):
if gEnableLog:
print(str)
logPath = os.path.join(gOutputDir, 'log.txt')
newFile = open(logPath, 'a+')
newFile.write(str + '\n')
newFile.close()
def decodeHtmlSpecialCharacter(htmlStr):
specChars = {" ": "",
" ": "",
" ": "",
"<": "<",
">": ">",
"&": "&",
""": "\"",
"©": "®",
"×": "×",
"÷": "÷",
}
for key in specChars.keys():
htmlStr = htmlStr.replace(key, specChars[key])
return htmlStr
def repalceInvalidCharInFilename(filename):
specChars = {"\\": "",
"/": "",
":": "",
"*": "",
"?": "",
"\"": "",
"<": "小于",
">": "大于",
"|": " and ",
"&": " or ",
}
for key in specChars.keys():
filename = filename.replace(key, specChars[key])
return filename
# process html content to markdown content
def htmlContent2String(contentStr):
patternImg = re.compile(r'(<img.+?src=")(.+?)(".+ />)')
patternHref = re.compile(r'(<a.+?href=")(.+?)(".+?>)(.+?)(</a>)')
patternRemoveHtml = re.compile(r'</?[^>]+>')
resultContent = patternImg.sub(r'![image_mark](\2)', contentStr)
resultContent = patternHref.sub(r'[\4](\2)', resultContent)
resultContent = re.sub(patternRemoveHtml, r'', resultContent)
resultContent = decodeHtmlSpecialCharacter(resultContent)
return resultContent
def exportToMarkdown(exportDir, postdate, categories, title, content):
titleDate = postdate.strftime('%Y-%m-%d')
contentDate = postdate.strftime('%Y-%m-%d %H:%M:%S %z')
filename = titleDate + '-' + title
filename = repalceInvalidCharInFilename(filename)
filepath = os.path.join(exportDir, filename + '.markdown')
log(" >> save as " + filepath)
newFile = open(unicode(filepath, "utf8"), 'w')
newFile.write('---' + '\n')
newFile.write('layout: post' + '\n')
newFile.write('title: \"' + title + '\"\n')
newFile.write('date: ' + contentDate + '\n')
newFile.write('comments: true' + '\n')
newFile.write('categories: [' + categories + ']' + '\n')
newFile.write('tags: [' + categories + ']' + '\n')
newFile.write('description: \"' + title + '\"\n')
newFile.write('keywords: ' + categories + '\n')
newFile.write('---' + '\n\n')
newFile.write(content)
newFile.write('\n')
newFile.close()
def download(title, url, output):
# 下载文章,并保存为 markdown 格式
log(" >> download: " + url)
categories = ""
content = ""
postDate = datetime.datetime.now()
slow_down()
page = getHtml(url)
soup = BeautifulSoup(page)
manageDocs = soup.find_all("div", "article_manage")
for managerDoc in manageDocs:
categoryDoc = managerDoc.find_all("span", "link_categories")
if len(categoryDoc) > 0:
categories = categoryDoc[0].a.get_text().encode('UTF-8').strip()
postDateDoc = managerDoc.find_all("span", "link_postdate")
if len(postDateDoc) > 0:
postDateStr = postDateDoc[0].string.encode('UTF-8').strip()
postDate = datetime.datetime.strptime(
postDateStr, '%Y-%m-%d %H:%M')
contentDocs = soup.find_all(id="article_content")
for contentDoc in contentDocs:
htmlContent = contentDoc.prettify().encode('UTF-8')
content = htmlContent2String(htmlContent)
exportToMarkdown(output, postDate, categories, title, content)
def getPageUrlList(url):
page = getHtml(url)
soup = BeautifulSoup(page)
lastArticleHref = None
pageListDocs = soup.find_all(id="papelist")
for pageList in pageListDocs:
hrefDocs = pageList.find_all("a")
if len(hrefDocs) > 0:
lastArticleHrefDoc = hrefDocs[len(hrefDocs) - 1]
lastArticleHref = lastArticleHrefDoc["href"].encode('UTF-8')
if not lastArticleHref:
return []
print(" > last page href:" + lastArticleHref)
lastPageIndex = lastArticleHref.rfind("/")
lastPageNum = int(lastArticleHref[lastPageIndex + 1:])
urlInfo = "http://blog.csdn.net" + lastArticleHref[0:lastPageIndex]
pageUrlList = []
for x in xrange(1, lastPageNum + 1):
pageUrl = urlInfo + "/" + str(x)
pageUrlList.append(pageUrl)
log(" > page " + str(x) + ": " + pageUrl)
log("total pages: " + str(len(pageUrlList)) + "\n")
return pageUrlList
def getArticleList(url):
# 获取所有的文章的 url/title
pageUrlList = getPageUrlList(url)
articleListDocs = []
for pageUrl in pageUrlList:
print(" > parsing page {0}".format(pageUrl))
slow_down() # 访问太快会不响应
page = getHtml(pageUrl)
soup = BeautifulSoup(page)
# 获取置顶文章
topArticleDocs = soup.find_all(id="article_toplist")
if topArticleDocs:
articleListDocs = articleListDocs + topArticleDocs
# 获取文章
articleDocs = soup.find_all(id="article_list")
if articleDocs:
articleListDocs = articleListDocs + articleDocs
break
artices = []
topTile = "[置顶]"
for articleListDoc in articleListDocs:
linkDocs = articleListDoc.find_all("span", "link_title")
for linkDoc in linkDocs:
# print(linkDoc.prettify().encode('UTF-8'))
link = linkDoc.a
url = link["href"].encode('UTF-8')
title = link.get_text().encode('UTF-8')
title = title.replace(topTile, '').strip()
oneHref = "http://blog.csdn.net" + url
#log(" > title:" + title + ", url:" + oneHref)
artices.append([oneHref, title])
log("total articles: " + str(len(artices)) + "\n")
return artices
def exportBlog(username, output):
url = "http://blog.csdn.net/" + username
path = os.path.join(output, username)
if not os.path.exists(path):
os.makedirs(path)
log(" >> user name: " + username)
log(" >> output dir: " + path)
log("start export...")
articleList = getArticleList(url)
totalNum = len(articleList)
log("start downloading...")
currentNum = 0
for article in articleList:
currentNum = currentNum + 1
strPageTemp = "[{0}/{1}] : {2}".format(
currentNum, totalNum, article[1])
log(strPageTemp)
download(article[1], article[0], path)
break
#=============================================================================
# 程序入口
#=============================================================================
# set your CSDN username
gUsername = "kesalin"
# set output dir
gOutputDir = "csdn_posts"
gEnableLog = True
if __name__ == '__main__':
exportBlog(gUsername, gOutputDir)