-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.py
executable file
·225 lines (214 loc) · 11 KB
/
crawler.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
#-*- coding:utf-8 -*-
import urllib2
import json
import time
#cookie_dict ={'_T_WM':'83ae70af166c701e5e05ea8538bceef4', 'gsid_CTandWM':'4u25CpOz5rGjCdIIGoV3nlUSq2R', 'H5_INDEX':'2', 'H5_INDEX_TITLE':'thu_Zengxs', 'SSOLoginState':'1464261968', 'SUB':'_2A256Qq0ADeRxGeNM6VEU8ijNzDuIHXVZzDNIrDV6PUJbrdBeLWLbkW1LHetrqciPXv8uSN5FGVARUDfGEK9KPg..', 'SUHB':'0N21z16Zx-NbhF'}
import re
# get user info by id through json analyst
def get_user_info_by_id(uid, retry=3):
url = 'http://m.weibo.cn/u/%s' % uid
success = False
attempts = 0
#print url
while not success:
try:
req = urllib2.Request(url,
headers={
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 ' +
'(KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'}
)
response = urllib2.urlopen(req)
data = response.read()
begin = data.find("""[{"mod_type":""")
end = data.find("""},'common':""")
body = data[begin:end]
body = json.loads(body, encoding = "gb2312")
#assert body[1]['mod_type'] != 'mod/empty'
assert 'id' in body[1]
success = True
except Exception as e:
if attempts < retry:
attempts += 1
time.sleep(2)
else:
#print e
return 0
#body = json.dumps(body[0], skipkeys = True, ensure_ascii = False, encoding = "gb2312", indent = 4)
nums = int(body[1]['mblogNum']), int(body[1]['attNum']), int(body[1]['fansNum'])
#print body
print nums
return body
# Another method that also works.
def get_user_info_by_id_v2(uid, cookie_dict, retry):
url = 'http://m.weibo.cn/users/'+uid+'/?'
success = False
attempts = 0
#print url
while not success:
try:
opener = urllib2.build_opener()
opener.addheaders.append(('Cookie', "; ".join('%s=%s' % (k,v) for k,v in cookie_dict.items())))
req = urllib2.Request(url,
headers={
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/601.1.56 (KHTML, like Gecko) Version/9.0 Safari/601.1.56',
#'Connection':'keep-alive'
#'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 ' +
#'(KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
}
)
response = opener.open(req)
data = response.read()
assert '昵称' in data
success = True
except Exception as e:
if attempts < retry:
attempts += 1
time.sleep(5)
else:
#print e
return 0
body = data
return body
# get posts by keyword by accessing the Weibo in-built search engine.
def get_weibo_by_keyword(keyword, page):
if page > 1:
url = 'http://m.weibo.cn/main/pages/index?containerid=100103type%3D1%26q%3D'+keyword+'&type=all&queryVal='+keyword+'&luicode=20000174&title='+keyword+'&page='+str(page)
else:
url = 'http://m.weibo.cn/main/pages/index?containerid=100103type%3D1%26q%3D'+keyword+'&type=all&queryVal='+keyword+'&luicode=20000174&title='+keyword
#print url
req = urllib2.Request(url,
headers={
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 ' +
'(KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'}
)
response = urllib2.urlopen(req)
data = response.read()
try:
body = json.loads(data)
weibos = body[1]['card_group'][0]['card_group']
weibolist = [w['mblog'] for w in weibos]
return weibolist
except Exception as e:
pass
# Weibo Topics is a place held by a host for users to join in the discussion of some specific topics. Host is also a weibo users.
# We called users who post under this topic the followers.
# This implements a function for grabbing the posts from a specific theme and extracting their owners' info..
def get_weibo_by_theme(container_id1, uid, cookie_dict, page = 1, retry = 3, last_since_id = None, next_since_id = None):
#url1 = 'http://m.weibo.cn/p/index?containerid='+container_id1+'&page='+str(page)
if page == 1:
url1 = 'http://m.weibo.cn/page/pageJson?containerid='+container_id1+'&containerid='+container_id1+'&uid='+uid+'&page=1'
else:
url1 = 'http://m.weibo.cn/page/pageJson?containerid=&containerid=' + container_id1 + '&uid=' + uid +'&from=feed&luicode=10000011&lfid=10730319c366f62380fecd399270159fcc2184_-_ext_intro&v_p=11&ext=&fid='+container_id1+'&uicode=10000011&next_cursor={%22last_since_id%22:'+last_since_id+',%22res_type%22:1,%22next_since_id%22:'+next_since_id+'}&page='+str(page)
#print url1
# /page/pageJson?containerid=&containerid=23053010080819c366f62380fecd399270159fcc2184__timeline__mobile_info_-_pageapp%3A23055763d3d983819d66869c27ae8da86cb176&uid=5223526177&from=feed&luicode=10000011&lfid=10730319c366f62380fecd399270159fcc2184_-_ext_intro&v_p=11&ext=&fid=23053010080819c366f62380fecd399270159fcc2184__timeline__mobile_info_-_pageapp%3A23055763d3d983819d66869c27ae8da86cb176&uicode=10000011&next_cursor={%22last_since_id%22:3981637848790627,%22res_type%22:1,%22next_since_id%22:3981630722841857}&page=2
#print url1
success = False
attempts = 0
body = 0
#print url1
while not success:
try:
opener = urllib2.build_opener()
opener.addheaders.append(('Cookie', "; ".join('%s=%s' % (k,v) for k,v in cookie_dict.items())))
req = urllib2.Request(url1,
headers={
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 ' +
'(KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
#, 'Cache-Control': 'max-age=0'
}
)
response = opener.open(req)
data = response.read()
body = json.loads(data)
assert len(body['cards']) > 0
success = True
except Exception as e:
if attempts < retry:
attempts += 1
time.sleep(3)
else:
return 0;
weibos = body['cards'][0]['card_group']
cursor = json.loads(body['next_cursor'])
Last = str(cursor['last_since_id'])
Next = str(cursor['next_since_id'])
users = [(k['mblog']['user']['screen_name'], str(k['mblog']['user']['id']), k['mblog']['user']['gender']) for k in weibos]
#weibos = body[1]['card_group'][0]['card_group']
#weibolist = [w['mblog'] for w in weibos]
#return [theme, weibos, users]
return [users, Last, Next]
# Each topic has its own fans. A user becomes fans by clicking the 'focus' button on the topic.
# This implementation can grab those fans's info.
def get_fans_by_theme(container_id, uid, cookie_dict, page = 2, retry = 3):
url = 'http://m.weibo.cn/p/index?containerid='+container_id+'&containerid='+container_id+'&title=粉丝&uid='+uid+'&page='+str(page)
success = False
attempts = 0
#print url
while not success:
try:
opener = urllib2.build_opener()
opener.addheaders.append(('Cookie', "; ".join('%s=%s' % (k,v) for k,v in cookie_dict.items())))
req = urllib2.Request(url,
headers={
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/601.1.56 (KHTML, like Gecko) Version/9.0 Safari/601.1.56',
#'Connection':'keep-alive'
#'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 ' +
#'(KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
}
)
response = opener.open(req)
data = response.read()
body = json.loads(data)
#assert body[1]['mod_type'] != 'mod/empty'
assert 'card_group' in body[1].keys()
success = True
except Exception as e:
if attempts < retry:
attempts += 1
time.sleep(2)
else:
#print e
return 0
users_list = body[1]['card_group'][0]['card_group']
users = [(k['user']['screen_name'], str(k['user']['id']), k['user']['location'][0:2], k['user']['gender']) for k in users_list]
return [users_list, users]
# Get user's fans and follows by sepecifying its uid.
def get_focus_or_fans_by_id(uid, cookie_dict, opt, page = 1, retry = 3):
if opt == 'focus':
url = 'http://m.weibo.cn/page/json?containerid=100505'+uid+'_-_FOLLOWERS&page='+str(page)
elif opt == 'fans':
url = 'http://m.weibo.cn/page/json?containerid=100505'+uid+'_-_FANS&page='+str(page)
success = False
attempts = 0
#print url
while not success:
try:
opener = urllib2.build_opener()
opener.addheaders.append(('Cookie', "; ".join('%s=%s' % (k,v) for k,v in cookie_dict.items())))
req = urllib2.Request(url,
headers={
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/601.1.56 (KHTML, like Gecko) Version/9.0 Safari/601.1.56',
#'Connection':'keep-alive'
#'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 ' +
#'(KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
}
)
response = opener.open(req)
data = response.read()
body = json.loads(data)
#assert body[1]['mod_type'] != 'mod/empty'
#assert body['count'] != None
success = True
except Exception as e:
if attempts < retry:
attempts += 1
time.sleep(3)
else:
#print e
return [None, None]
num = body['count']
if num == None:
return [None, None]
users_list = body['cards'][0]['card_group']
users = [str(k['user']['id']) for k in users_list]
return [num, users]