CSDN博客搬家到WordPress

2020年05月28日 阅读数:55
这篇文章主要向大家介绍CSDN博客搬家到WordPress,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

一直想把CSDN上写的博客,搬家移植到本身的WordPress博客,网上介绍了一些方法,例如先把CSDN博客导入博客园,而后从博客园导出xml文件,最后用WordPress导入工具导入进WordPress博客,操做步骤太繁杂。javascript

因而周末在家本身动手用Python写了爬虫脚本,利用WordPress自定义的投稿功能(wp_insert_post),导入进WordPress博客,步骤只有简单的两步:html

步骤1:Python脚本爬取CSDN博客,包括博客标题,正文,发布日期,浏览数,标签java

步骤2:利用WordPress投稿功能插入到WordPress,把全部CSDN博客导入WordPresspython

 

Python完整代码:c++

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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/env python
# -*- coding:utf8 -*-
 
'''
author  :    yanggang@mimvp.com
date    :    2015-12-01
blog    :    http://blog.mimvp.com
demo    :    http://blog.mimvp.com/category/csdn_blog/
 
安装库文件:
1. MySQLdb,链接MySQL数据库,修改浏览次数
2. bs4,    格式化HTML,抓取过滤CSDN网页内容
 
 
WordPress博客POST文章接口API
 
http://blog.mimvp.com/tougao/
 
查询自动POST文章ID:
select id from wp_posts where post_title like '【米扑代理】%'
 
修改POST文章浏览数:
update wp_postmeta set meta_value=meta_value+100 where meta_key='views' and post_id in (select id from wp_posts where post_title like '【米扑代理】%');
 
修改浏览次数小于100
select post_id, meta_value from wp_postmeta where  meta_key='views' and meta_value < 100 and post_id in (select id  from wp_posts where post_status = 'publish');
 
'''
 
import time, datetime
import os, random
import urllib, urllib2
import base64
import bs4
import MySQLdb
 
import sys
reload (sys)
sys.setdefaultencoding( 'utf-8' )
 
# 排除要抓取的CSDN博客连接
EXCLUDE_BLOG_URL = [
                         'http://blog.csdn.net/sunboy_2050/article/details/45868613' ,
                         'http://blog.csdn.net/sunboy_2050/article/details/49449319' ,
                         'http://blog.csdn.net/sunboy_2050/article/details/48622023' ,
                         'http://blog.csdn.net/sunboy_2050/article/details/47858847' ,
                     ]
 
# 根据CSDN博客标题打标签tags
CATEGORY_DICT = {
                 'Python'    :   1 ,
                 'C/C++'     :   2 ,
                 '网络常识'   :   3 ,
                 'Algrithm'  :   11 ,
                 'Clojure'   :   4 ,
                 'CSDN'      :   5 ,
                 'Git/SVN'   :   6 ,
                 'Go'        :   7 ,
                 'HTML/CSS/JS'  :   8 ,
                 'iOS/Android'  :   9 ,
                 'Java/JSP'     :   113 ,
                 'Linux/Unix'   :   95 ,
                 'MacBook'      :   111 ,
                 'Nginx/Apache' :   112 ,
                 'PHP'          :   777 ,
                 'SQL/NoSQL'    :   114 ,
                 'Storm/Hadoop' :   115 ,
                 'WP技巧'        :   12 ,
                 '产品经理'       :   116 ,
                 '创业邦'        :   3 ,
                 '理财'          :   117 ,
                 '生活小札'       :   4 ,
                 '科技资讯'       :   10 ,
                 '米扑代理'       :   118 ,
                 '系统架构'       :   97 ,
                 '软件测试'       :   119 ,
                 
                 'leetcode'   :   11 ,
                 '链表'        :   11 ,
                 '算法'        :   11 ,
                 'leetcode'   :   11 ,
                 'django'     :   1 ,
                 'tornado'    :   1 ,
                 'c语言'       :   2 ,
                 'c++'        :   2 ,
                 'c#'         :   2 ,
                 'vc'         :   2 ,
                 'qt'         :   2 ,
                 '网络'     :   3 ,
                 '算法'     :   11 ,
                 'Git'     :   6 ,
                 'SVN'     :   6 ,
                 '版本控制'  :   6 ,
                 'HTML'    :   8 ,
                 'CSS'     :   8 ,
                 'JS'      :   8 ,
                 'javascript' :   8 ,
                 'iOS'     :   9 ,
                 'Android' :   9 ,
                 'Java'    :   113 ,
                 'JSP'     :   113 ,
                 'JVM'     :   113 ,
                 'Spring'  :   113 ,
                 'Eclipse' :   113 ,
                 'Linux'   :   95 ,
                 'Unix'    :   95 ,
                 'Ubuntu'  :   95 ,
                 'CentOS'  :   95 ,
                 'Shell'   :   95 ,
                 'AWK'     :   95 ,
                 'vim'     :   95 ,
                 'Nginx'   :   112 ,
                 'Apache'  :   112 ,
                 'Tomcat'  :   112 ,
                 '数据库'   :   114 ,
                 'SQL'     :   114 ,
                 'NoSQL'   :   114 ,
                 'MySQL'   :   114 ,
                 'Redis'   :   114 ,
                 'Memcache' :   114 ,
                 'mongo'   :   114 ,
                 'sqlite'  :   114 ,
                 'WP'      :   12 ,
                 'WordPress'     :   12 ,
                 '软件'     :   119 ,
                 '测试'     :   119 ,
                 'Storm'   :   115 ,
                 'Hadoop'  :   115 ,
              }
 
# CSDN博客用urllib2 + header抓取不了,改用了curl爬取网页
headers = {
             'Use-Agent' :   'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36' ,
             'Cookie'    :   'bdshare_firstime=1430381399166; uuid_tt_dd=-4349350129693538496_20150430; __gads=ID=b549fb1461110ca8:T=1430381399:S=ALNI_MbhiN2nMgWWiXPP25667Pq-BPZf5g; CloudGuest=dbieDkbnW7cz5P9qimYEjpecak8Udv0BPR8Iflg0PlBd3HR1Wj+RyQQksR2cDE9ab/hPXNGjFpuKsRe1dFjVJjY+mf3bfeWiP6kN0TKk1rY6g5SOuowPs/8F5FJJBdddW71JZ7rp4Q9b8DsLk2TASIPHLj3iL599bPUGKga0mRsaTJi0td73QBZaNlY7+VAl; __qca=P0-127195741-1432118980220; lzstat_uv=29735680241046032471|2955225@3582543@2675686@3411160; __utma=1722629.287164464.1430711804.1438215324.143920134.51; __utmz=1722629.14380001130.49.44.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; UN=Sunboy_2050; UE="yanggang_2050@19.com"; __message_district_code=110000; uuid=92c68418-b2bd-41bc-846d-a1e4e9cf094; _ga=GA1.2.287164464.1430711804; scvh=2011-09-09+17%3a11%3a33+003; FullCookie=1; ViewMode=list; avh=8471508%2c298041%2c45868613%2c2659431%2c17398807; __message_sys_msg_id=0; __message_gu_msg_id=0; __message_cnel_msg_id=0; __message_in_school=0; lzstat_ss=3875140043_20_1448553416_2955225; dc_tos=nyewqw; dc_session_id=1448524616939' ,
             'Host'      :   'blog.csdn.net' ,
             'DNT'       :   1 ,
             'Cache-Control'     :   'max-age=0' ,
             'Connection'        :   'keep-alive' ,
             'Accept'            :   'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' ,
             'Accept-Encoding'   :   'gzip, deflate, sdch' ,
             'Accept-Language'   :   'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4' ,
             'DNT'    :   1 ,
           }
 
# hangzhou of 115.29.237.28
PROXY_MYSQL_SERVER = {
                          "host"     :   "localhost" ,
                          "port"     :   3306 ,
                          "user"     :   "root" ,
                          "passwd"   :