-
Notifications
You must be signed in to change notification settings - Fork 2
/
getWeiboPage.py
261 lines (238 loc) · 7.1 KB
/
getWeiboPage.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib
import urllib2
import sys
import time
import CommonWeiboMsg
import CompanyWeiboMsg
import OfficeWeiboMsg
import utility
import logging
reload(sys)
sys.setdefaultencoding('utf-8')
class getWeiboPage:
def __init__(self):
self.charset = 'utf-8'
self.page_num = 1 # 微博总共有多少页
self.flag = 0 # 标记是否已经获取页数
self.num = 0 # 标记一页中的第几分页
self.version = -1; # 微博的版本(普通用户版0, 企业用户版1, 新浪官方版2)
self.comm_wbmsg = CommonWeiboMsg.CommonWeiboMsg();
self.comp_wbmsg = CompanyWeiboMsg.CompanyWeiboMsg();
self.offi_wbmsg = OfficeWeiboMsg.OfficeWeiboMsg();
self.wbmsg = None;
# 为微博解析选择版本
def select_version(self):
if(self.version == 0):
self.comm_wbmsg.init_user(self.uid);
self.wbmsg = self.comm_wbmsg;
elif(self.version == 1):
self.comp_wbmsg.init_user(self.uid);
self.wbmsg = self.comp_wbmsg;
elif(self.version == 2):
self.offi_wbmsg.init_user(self.uid);
self.wbmsg = self.offi_wbmsg;
else:
self.wbmsg = None;
def set_uid(self,puid):
self.uid = puid
def get_uid(self):
return self.uid
# 预处理,包括:获取page_id,选择微博解析类型,微博总页数.成功返回True,否则返回False
def preprocess(self, uid):
# 获取 page_id
self.body = {
'__rnd':'',
'_k':'',
'_t':'0',
'count':'15',
'end_id':'',
'max_id':'',
'page':1,
'pagebar':'',
'pre_page':'0',
'uid':uid
};
url = 'http://weibo.com/u/' + uid + '?profile_ftype=1';
content = self.download(url);
if( content == None ):
logging.info('%s 页面加载失败', url);
return False;
tag = "$CONFIG['page_id']='";
pos1 = content.find(tag) + len(tag);
if( pos1 == -1):
logging.info('%s page_id解析失败', uid);
return False;
pos2 = content.find("'", pos1);
self.page_id = content[pos1:pos2];
# 获取微博总页数,以及版本选择
if(not self.get_totallpage_num(content, uid)): #微博总页数获取失败
logging.info('%s 微博总页数解析失败', uid);
return False;
self.select_version();
# 设置页面url加载的参数
self.body = {
'is_search':'0',
'visible':'0',
'is_tag':'0',
'profile_ftype':1,
'pagebar':'',
'pre_page':'0',
'page':1
};
return True;
# 处理一个 uid 的微博知识
def get_msg(self, uid):
self.flag = 0
self.uid = uid;
if( not self.preprocess(uid) ):
return;
url = self.get_url()
for i in range(1, self.page_num+1):
self.body['page'] = i
if( not self.get_firstpage(url, uid) ):
break;
if( not self.get_secondpage(url, uid) ):
break;
if( not self.get_thirdpage(url, uid) ):
break;
# 判断用户是否存在,存在返回True,否则返回False
def user_exist(self, content):
if(content.find('<title>错误提示') != -1):
return False;
return True;
# 获取新浪官方微博总数,成功返回True,否则返回False
def totalpage_office(self, content):
pos1 = content.find('<table class="W_tc"');
if( pos1 != -1 ):
pos2 = content.find('<\/table>', pos1);
if(pos2 != -1):
slug = content[pos1:pos2];
bTag = 'mod=weibo"><strong class="">';
pos1 = slug.find(bTag) + len(bTag);
pos2 = slug.find('<\/strong>', pos1);
temp = slug[pos1:pos2];
if(temp.isdigit()):
self.page_num = int(temp);
return True;
return False;
# 获取企业用户微博总数,成功返回True,否则返回False
def totalpage_company(self, content):
pos1 = content.find('class="user_atten clearfix">');
if( pos1 != -1):
pos2 = content.find('<\/ul>', pos1);
if(pos2 != -1):
slug = content[pos1:pos2];
eTag = '<\/strong><span>微博';
pos2 = slug.find(eTag);
if(pos2 != -1):
bTag = '<strong>';
pos1 = slug.rfind(bTag, 0, pos2);
if(pos1 != -1):
pos1 = pos1 + len(bTag);
temp = slug[pos1:pos2];
if(temp.isdigit()):
self.page_num = int(temp);
return True;
return False;
# 获取一般用户微博总数,成功返回True,否则返回False
def totalpage_common(self, content):
tag1 = '<strong node-type="weibo">'
pos1 = content.find(tag1)+len(tag1)
tag2 = '<\/strong>'
pos2 = content.find(tag2,pos1)
temp = content[pos1:pos2]
if(temp.isdigit()):
self.page_num = int(temp);
return True;
return False;
# 获取微博页面的总页数,成功True,否则返回False
def get_totallpage_num(self, content, uid):
version = -1;
try:
if(self.user_exist(content)):
if(not self.totalpage_common(content)):
if(not self.totalpage_company(content)):
if(not self.totalpage_office(content)):
logging.warning('%s 无法解析微博总页数', uid);
self.version = -1;
return False;
else:
self.version = 2;
#return False;
else:
self.version = 1;
else:
self.version = 0;
else:
logging.info('%s 用户不存在', uid);
return False;
except Exception,e:
logging.exception("%s 获取总页数失败: " + str(e), uid);
writer = utility.createFile('error', uid);
writer.write(content);
writer.close();
return False;
self.page_num = self.page_num / 45 + 1
utility.iprint( self.get_uid() + ':微博总共有 ' + str(self.page_num) + ' 页' )
logging.info(uid + " 共有 %d 页微博", self.page_num);
return True;
# 获取第一分页微博,不用继续处理后续微博返回False
def get_firstpage(self, url, uid):
self.body['pre_page'] = self.body['page']-1
self.num = 1
content = self.download(url)
if( content == None ):
return True;
return self.wbmsg.get_content(content)
# 获取第二分页微博,不用继续处理后续微博返回False
def get_secondpage(self, url, uid):
self.body['count'] = '15'
self.body['pagebar'] = '0'
self.body['pre_page'] = self.body['page']
self.num = 2
content = self.download(url)
if(content == None):
return True;
return self.wbmsg.get_content(content)
# 获取第三分页微博,不用继续处理后续微博返回False
def get_thirdpage(self, url, uid):
self.body['count'] = '15'
self.body['pagebar'] = '1'
self.body['pre_page'] = self.body['page']
self.num = 3
content = self.download(url);
if(content == None):
return True;
result = self.wbmsg.get_content(content)
utility.iprint(self.get_uid() + ':获取第' + str(self.body['page']) + '页微博成功')
return result;
# 获取用户微博页面的url
def get_url(self):
url = 'http://weibo.com/p/' + self.page_id + '/';
if( self.version == 0):
url += 'weibo?';
else:
url += 'mblog?';
return url
# 下载网页,并进行解码,编码
def download(self, url):
try:
url = url + urllib.urlencode(self.body)
req = urllib2.Request(url)
result = urllib2.urlopen(req)
text = result.read()
except Exception, e:
logging.error('%s 网页下载失败: ' + str(e), url);
return None;
try:
content = text.decode(self.charset, 'ignore');
content = eval("u'''" + content + "'''").encode(self.charset);
except Exception, e:
logging.error('%s 网页解码失败: ' + str(e), url);
return None;
return content
if __name__ == '__main__':
#getWeiboPage = getWeiboPage();
pass;