本文实例讲述了Python实现新浪博客备份的方法。分享给大家供大家参考,具体如下:
Python2.7.2版本实现,推荐在IDE中运行。
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
|
# -*- coding:UTF-8 -*- #
'''
Created on 2011-12-18
@author: Ahan
'''
import re
import sys
import os
import time
import socket
import locale
import datetime
import codecs
from urllib import urlopen
#正则表达式定义
#匹配博文目录链接
pattern1 = u """<a href="(http:.*?)">博文目录</a>"""
prog1 = re. compile (pattern1)
#匹配博文标题链接
pattern2 = u """<a target='_blank'>图片链接
pattern5 = u """(src="[^"]+"( real_src ="([^"]+)"))"""
prog5 = re. compile (pattern5)
def read_date_from_url(url):
"""以Unicode形式返回从url上读取的所有数据
"""
try :
data = ""
request = urlopen(url)
while True :
s = request.read( 1024 )
if not s:
break
data + = s
return unicode (data)
except :
print '读取数据时出错'
print "Unexpected error:" , sys.exc_info()[ 0 ],sys.exc_info()[ 1 ]
return None
finally :
if request:
request.close()
def save_to_file(url,filename,blog_address):
"""url为博文地址,filename为要保存的文件名,默认后缀为html
"""
#如果文件夹不存在则创建文件夹
if os.path.exists(blog_address) = = False :
os.makedirs(blog_address)
#去掉文件名中的非法字符
filename = ReplaceBadCharOfFileName(filename)
file_no = 0
while os.path.isfile(blog_address + '/' + filename + '.html' ) = = True :
filename = filename + '(' + file_no.__str__() + ')'
file_no + = 1
text = read_date_from_url(url)
text = _filter(text)
#将图片保存到本地
result = prog5.findall(text)
i = 1
for pic in result:
folder = blog_address + '/' + filename + '/'
pic_name = 'image' + i.__str__() + '.gif'
if os.path.exists(folder) = = False :
os.makedirs(folder)
try :
url_file = urlopen(pic[ 2 ])
pic_file = codecs. open (folder + pic_name, 'wb' )
while True :
s = url_file.read( 1024 )
if not s:
break
pic_file.write(s)
pic_file.close()
url_file.close()
except :
print '噢,保存图片的时候出现问题了,跳过此张图片...'
print "Unexpected error:" , sys.exc_info()[ 0 ],sys.exc_info()[ 1 ]
else :
print '保存图片成功...'
#替换正文中的图片地址
text = text.replace(pic[ 0 ], unicode ( "src=\"" + filename + "/" + pic_name + "\"" + pic[ 1 ]), 1 )
i = i + 1
blog_file = codecs. open (blog_address + '/' + filename + '.html' , 'wb' )
blog_file.write(text)
blog_file.close()
#提取文本中的正文部分
def _filter(t):
"""提取文本中的正文部分,返回Unicode形式的字符串
"""
result = prog4.search(t)
if result is not None :
return u '<html><head></head><body>' + unicode (result.group()) + u '</dody></html>'
else :
raise Exception( '噢,提取正文出错了……' )
#去掉文件名的不合法字符
def ReplaceBadCharOfFileName(filename):
filename = filename.replace( " " ,"")
filename = filename.replace( "\\", " ")
filename = filename.replace( "/" , "")
filename = filename.replace( ":" , "")
filename = filename.replace( "*" , "")
filename = filename.replace( "?" , "")
filename = filename.replace( "<" , "")
filename = filename.replace( ">" , "")
filename = filename.replace( "|" , "")
filename = filename.replace( "&" ,"")
filename = filename.replace( ";" ,"")
return filename
#主函数
if __name__ = = '__main__' :
#准备阶段
blog_no = 1 #博文编号
begin = 1 #起始博文
end = 0 #结束博文
page = 0 #页码
saved = 0 #成功保存的篇数
timeout = 60 * 5 #超时设为5分钟
socket.setdefaulttimeout(timeout) #这里对整个socket层设置超时时间。后续文件中如果再使用到socket,不必再设置
blog_address = raw_input ( "请输入您的博客地址(输入最后部分即可,比如您的博客地址是http://blog.sina.com.cn/jiangafu,只要输入jiangafu):" )
blog_address = blog_address.replace( '\r' ,'')
begin = raw_input ( '从第几篇开始:' )
begin = locale.atoi(begin)
while begin< = 0 :
begin = raw_input ( '请输入大于0的数:' )
begin = locale.atoi(begin)
end = raw_input ( '到第几篇结束(到最后请输入0):' )
end = locale.atoi(end)
while end< 0 :
end = raw_input ( '请输入大于等于0的数:' )
end = locale.atoi(end)
if end = = 0 :
print '您的博客地址是:http://blog.sina.com.cn/' + blog_address + ',保存第' + begin.__str__() + '篇到最后一篇博文'
else :
print '您的博客地址是:http://blog.sina.com.cn/' + blog_address + ',保存第' + begin.__str__() + '篇到第' \
+ end.__str__() + '篇的博文'
starttime = datetime.datetime.now()
text = read_date_from_url( 'http://blog.sina.com.cn/' + blog_address)
time.sleep( 0.5 )
#提取“博文目录”的url
result = prog1.search(text)
if result is not None :
print '博文目录地址:' , result.group( 1 )
text = read_date_from_url(result.group( 1 ))
time.sleep( 0.4 )
else :
print '提取博文目录地址失败'
#终止程序运行
sys.exit()
#查找每一页的全部博文,分析、提取、保存
while True :
page + = 1
print '开始备份第' , page , '页'
#匹配该页的所有博文地址
result = prog2.findall(text)
#循环下载本页每篇博文
for blog in result:
if blog_no < begin:
blog_no + = 1
elif end ! = 0 and blog_no > end:
break
else :
try :
save_to_file(blog[ 1 ], unicode (blog[ 0 ]),blog_address)
except :
print '噢,保存第' ,blog_no, '篇博文' ,blog[ 0 ], '的时候出现问题了,跳过...'
blog_no + = 1
print "Unexpected error:" , sys.exc_info()[ 0 ],sys.exc_info()[ 1 ]
else :
print '成功保存了第' , blog_no, '篇博文:' , blog[ 0 ]
blog_no + = 1
saved + = 1
time.sleep( 0.4 )
#判断是否有下一页
result = prog3.search(text)
if result is not None :
text = read_date_from_url(result.group( 1 ))
else :
print '这是最后一页'
break
print '博客备份完成一共备份' ,saved, '篇博文'
print '共用时:' ,datetime.datetime.now() - starttime
raw_input ( '按回车键退出...' )
|
希望本文所述对大家Python程序设计有所帮助。