-
Notifications
You must be signed in to change notification settings - Fork 100
/
linkedinSpider.py
397 lines (365 loc) · 19 KB
/
linkedinSpider.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# encoding=utf-8
# ----------------------------------------
# 语言:Python2.7
# 日期:2017-03-24
# 作者:九茶<http://blog.csdn.net/bone_ace>
# 功能:根据公司名称抓取员工的LinkedIn数据
# ----------------------------------------
import sys
import copy
import time
from urllib import unquote
import requests
from urllib import quote
import re
from lxml import etree
reload(sys)
sys.setdefaultencoding('utf8')
CREDIT_GRADE = { # 芝麻信用
'EXCELLENT': '极好',
'VERY_GOOD': '优秀',
'GOOD': '良好',
'ACCEPTABLE': '中等',
'POOR': '较差'
}
LINKS_FINISHED = [] # 已抓取的linkedin用户
def login(laccount, lpassword):
""" 根据账号密码登录linkedin """
s = requests.Session()
r = s.get('https://www.linkedin.com/uas/login')
tree = etree.HTML(r.content)
loginCsrfParam = ''.join(tree.xpath('//input[@id="loginCsrfParam-login"]/@value'))
csrfToken = ''.join(tree.xpath('//input[@id="csrfToken-login"]/@value'))
sourceAlias = ''.join(tree.xpath('//input[@id="sourceAlias-login"]/@value'))
isJsEnabled = ''.join(tree.xpath('//input[@name="isJsEnabled"]/@value'))
source_app = ''.join(tree.xpath('//input[@name="source_app"]/@value'))
tryCount = ''.join(tree.xpath('//input[@id="tryCount"]/@value'))
clickedSuggestion = ''.join(tree.xpath('//input[@id="clickedSuggestion"]/@value'))
signin = ''.join(tree.xpath('//input[@name="signin"]/@value'))
session_redirect = ''.join(tree.xpath('//input[@name="session_redirect"]/@value'))
trk = ''.join(tree.xpath('//input[@name="trk"]/@value'))
fromEmail = ''.join(tree.xpath('//input[@name="fromEmail"]/@value'))
payload = {
'isJsEnabled': isJsEnabled,
'source_app': source_app,
'tryCount': tryCount,
'clickedSuggestion': clickedSuggestion,
'session_key': laccount,
'session_password': lpassword,
'signin': signin,
'session_redirect': session_redirect,
'trk': trk,
'loginCsrfParam': loginCsrfParam,
'fromEmail': fromEmail,
'csrfToken': csrfToken,
'sourceAlias': sourceAlias
}
s.post('https://www.linkedin.com/uas/login-submit', data=payload)
return s
def get_linkedin_url(url, s):
""" 百度搜索出来的是百度跳转链接,要从中提取出linkedin链接 """
try:
r = s.get(url, allow_redirects=False)
if r.status_code == 302 and 'Location' in r.headers.keys() and 'linkedin.com/in/' in r.headers['Location']:
return r.headers['Location']
except Exception, e:
print 'get linkedin url failed: %s' % url
return ''
def parse(content, url):
""" 解析一个员工的Linkedin主页 """
content = unquote(content).replace('"', '"')
profile_txt = ' '.join(re.findall('(\{[^\{]*?profile\.Profile"[^\}]*?\})', content))
firstname = re.findall('"firstName":"(.*?)"', profile_txt)
lastname = re.findall('"lastName":"(.*?)"', profile_txt)
if firstname and lastname:
print '姓名: %s%s Linkedin: %s' % (lastname[0], firstname[0], url)
summary = re.findall('"summary":"(.*?)"', profile_txt)
if summary:
print '简介: %s' % summary[0]
occupation = re.findall('"headline":"(.*?)"', profile_txt)
if occupation:
print '身份/职位: %s' % occupation[0]
locationName = re.findall('"locationName":"(.*?)"', profile_txt)
if locationName:
print '坐标: %s' % locationName[0]
networkInfo_txt = ' '.join(re.findall('(\{[^\{]*?profile\.ProfileNetworkInfo"[^\}]*?\})', content))
connectionsCount = re.findall('"connectionsCount":(\d+)', networkInfo_txt)
if connectionsCount:
print '好友人数: %s' % connectionsCount[0]
sesameCredit_txt = ' '.join(re.findall('(\{[^\{]*?profile\.SesameCreditGradeInfo"[^\}]*?\})', content))
credit_lastModifiedAt = re.findall('"lastModifiedAt":(\d+)', sesameCredit_txt)
credit_grade = re.findall('"grade":"(.*?)"', sesameCredit_txt)
if credit_grade and credit_grade[0] in CREDIT_GRADE.keys():
credit_lastModifiedAt_date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(credit_lastModifiedAt[0][:10]))) if credit_lastModifiedAt else ''
print '芝麻信用: %s %s' % (CREDIT_GRADE[credit_grade[0]], ' 最后更新时间: %s' % credit_lastModifiedAt_date if credit_lastModifiedAt_date else '')
wechat_txt = ' '.join(re.findall('(\{[^\{]*?profile\.WeChatContactInfo"[^\}]*?\})', content))
wechat_image = re.findall('"qrCodeImageUrl":"(http.*?)"', wechat_txt)
wechat_name = re.findall('"name":"(.*?)"', wechat_txt)
if wechat_name:
print '微信昵称: %s %s' % (wechat_name[0], ' 二维码(链接): %s' % wechat_image[0].replace('=', '=').replace('&', '&') if wechat_image else '')
elif wechat_image:
print '微信二维码(链接): %s' % wechat_image[0].replace('=', '')
website_txt = ' '.join(re.findall('("included":.*?profile\.StandardWebsite",.*?\})', content))
website = re.findall('"url":"(.*?)"', website_txt)
if website:
print '个人网站: %s' % website[0]
educations = re.findall('(\{[^\{]*?profile\.Education"[^\}]*?\})', content)
if educations:
print '教育经历:'
for one in educations:
schoolName = re.findall('"schoolName":"(.*?)"', one)
fieldOfStudy = re.findall('"fieldOfStudy":"(.*?)"', one)
degreeName = re.findall('"degreeName":"(.*?)"', one)
timePeriod = re.findall('"timePeriod":"(.*?)"', one)
schoolTime = ''
if timePeriod:
startdate_txt = ' '.join(re.findall('(\{[^\{]*?"\$id":"%s,startDate"[^\}]*?\})' % timePeriod[0].replace('(', '\(').replace(')', '\)'), content))
enddate_txt = ' '.join(re.findall('(\{[^\{]*?"\$id":"%s,endDate"[^\}]*?\})' % timePeriod[0].replace('(', '\(').replace(')', '\)'), content))
start_year = re.findall('"year":(\d+)', startdate_txt)
start_month = re.findall('"month":(\d+)', startdate_txt)
end_year = re.findall('"year":(\d+)', enddate_txt)
end_month = re.findall('"month":(\d+)', enddate_txt)
startdate = ''
if start_year:
startdate += '%s' % start_year[0]
if start_month:
startdate += '.%s' % start_month[0]
enddate = ''
if end_year:
enddate += '%s' % end_year[0]
if end_month:
enddate += '.%s' % end_month[0]
if len(startdate) > 0 and len(enddate) == 0:
enddate = '现在'
schoolTime += ' %s ~ %s' % (startdate, enddate)
if schoolName:
fieldOfStudy = ' %s' % fieldOfStudy[0] if fieldOfStudy else ''
degreeName = ' %s' % degreeName[0] if degreeName else ''
print ' %s %s %s %s' % (schoolName[0], schoolTime, fieldOfStudy, degreeName)
position = re.findall('(\{[^\{]*?profile\.Position"[^\}]*?\})', content)
if position:
print '工作经历:'
for one in position:
companyName = re.findall('"companyName":"(.*?)"', one)
title = re.findall('"title":"(.*?)"', one)
locationName = re.findall('"locationName":"(.*?)"', one)
timePeriod = re.findall('"timePeriod":"(.*?)"', one)
positionTime = ''
if timePeriod:
startdate_txt = ' '.join(re.findall('(\{[^\{]*?"\$id":"%s,startDate"[^\}]*?\})' % timePeriod[0].replace('(', '\(').replace(')', '\)'), content))
enddate_txt = ' '.join(re.findall('(\{[^\{]*?"\$id":"%s,endDate"[^\}]*?\})' % timePeriod[0].replace('(', '\(').replace(')', '\)'), content))
start_year = re.findall('"year":(\d+)', startdate_txt)
start_month = re.findall('"month":(\d+)', startdate_txt)
end_year = re.findall('"year":(\d+)', enddate_txt)
end_month = re.findall('"month":(\d+)', enddate_txt)
startdate = ''
if start_year:
startdate += '%s' % start_year[0]
if start_month:
startdate += '.%s' % start_month[0]
enddate = ''
if end_year:
enddate += '%s' % end_year[0]
if end_month:
enddate += '.%s' % end_month[0]
if len(startdate) > 0 and len(enddate) == 0:
enddate = '现在'
positionTime += ' %s ~ %s' % (startdate, enddate)
if companyName:
title = ' %s' % title[0] if title else ''
locationName = ' %s' % locationName[0] if locationName else ''
print ' %s %s %s %s' % (companyName[0], positionTime, title, locationName)
publication = re.findall('(\{[^\{]*?profile\.Publication"[^\}]*?\})', content)
if publication:
print '出版作品:'
for one in publication:
name = re.findall('"name":"(.*?)"', one)
publisher = re.findall('"publisher":"(.*?)"', one)
if name:
print ' %s %s' % (name[0], ' 出版社: %s' % publisher[0] if publisher else '')
honor = re.findall('(\{[^\{]*?profile\.Honor"[^\}]*?\})', content)
if honor:
print '荣誉奖项:'
for one in honor:
title = re.findall('"title":"(.*?)"', one)
issuer = re.findall('"issuer":"(.*?)"', one)
issueDate = re.findall('"issueDate":"(.*?)"', one)
issueTime = ''
if issueDate:
issueDate_txt = ' '.join(re.findall('(\{[^\{]*?"\$id":"%s"[^\}]*?\})' % issueDate[0].replace('(', '\(').replace(')', '\)'), content))
year = re.findall('"year":(\d+)', issueDate_txt)
month = re.findall('"month":(\d+)', issueDate_txt)
if year:
issueTime += ' 发行时间: %s' % year[0]
if month:
issueTime += '.%s' % month[0]
if title:
print ' %s %s %s' % (title[0], ' 发行人: %s' % issuer[0] if issuer else '', issueTime)
organization = re.findall('(\{[^\{]*?profile\.Organization"[^\}]*?\})', content)
if organization:
print '参与组织:'
for one in organization:
name = re.findall('"name":"(.*?)"', one)
timePeriod = re.findall('"timePeriod":"(.*?)"', one)
organizationTime = ''
if timePeriod:
startdate_txt = ' '.join(re.findall('(\{[^\{]*?"\$id":"%s,startDate"[^\}]*?\})' % timePeriod[0].replace('(', '\(').replace(')', '\)'), content))
enddate_txt = ' '.join(re.findall('(\{[^\{]*?"\$id":"%s,endDate"[^\}]*?\})' % timePeriod[0].replace('(', '\(').replace(')', '\)'), content))
start_year = re.findall('"year":(\d+)', startdate_txt)
start_month = re.findall('"month":(\d+)', startdate_txt)
end_year = re.findall('"year":(\d+)', enddate_txt)
end_month = re.findall('"month":(\d+)', enddate_txt)
startdate = ''
if start_year:
startdate += '%s' % start_year[0]
if start_month:
startdate += '.%s' % start_month[0]
enddate = ''
if end_year:
enddate += '%s' % end_year[0]
if end_month:
enddate += '.%s' % end_month[0]
if len(startdate) > 0 and len(enddate) == 0:
enddate = '现在'
organizationTime += ' %s ~ %s' % (startdate, enddate)
if name:
print ' %s %s' % (name[0], organizationTime)
patent = re.findall('(\{[^\{]*?profile\.Patent"[^\}]*?\})', content)
if patent:
print '专利发明:'
for one in patent:
title = re.findall('"title":"(.*?)"', one)
issuer = re.findall('"issuer":"(.*?)"', one)
url = re.findall('"url":"(http.*?)"', one)
number = re.findall('"number":"(.*?)"', one)
localizedIssuerCountryName = re.findall('"localizedIssuerCountryName":"(.*?)"', one)
issueDate = re.findall('"issueDate":"(.*?)"', one)
patentTime = ''
if issueDate:
issueDate_txt = ' '.join(re.findall('(\{[^\{]*?"\$id":"%s"[^\}]*?\})' % issueDate[0].replace('(', '\(').replace(')', '\)'), content))
year = re.findall('"year":(\d+)', issueDate_txt)
month = re.findall('"month":(\d+)', issueDate_txt)
day = re.findall('"day":(\d+)', issueDate_txt)
if year:
patentTime += ' 发行时间: %s' % year[0]
if month:
patentTime += '.%s' % month[0]
if day:
patentTime += '.%s' % day[0]
if title:
print ' %s %s %s %s %s %s' % (title[0], ' 发行者: %s' % issuer[0] if issuer else '', ' 专利号: %s' % number[0] if number else '', ' 所在国家: %s' % localizedIssuerCountryName[0] if localizedIssuerCountryName else '', patentTime, ' 专利详情页: %s' % url[0] if url else '')
project = re.findall('(\{[^\{]*?profile\.Project"[^\}]*?\})', content)
if project:
print '所做项目:'
for one in project:
title = re.findall('"title":"(.*?)"', one)
description = re.findall('"description":"(.*?)"', one)
timePeriod = re.findall('"timePeriod":"(.*?)"', one)
projectTime = ''
if timePeriod:
startdate_txt = ' '.join(re.findall('(\{[^\{]*?"\$id":"%s,startDate"[^\}]*?\})' % timePeriod[0].replace('(', '\(').replace(')', '\)'), content))
enddate_txt = ' '.join(re.findall('(\{[^\{]*?"\$id":"%s,endDate"[^\}]*?\})' % timePeriod[0].replace('(', '\(').replace(')', '\)'), content))
start_year = re.findall('"year":(\d+)', startdate_txt)
start_month = re.findall('"month":(\d+)', startdate_txt)
end_year = re.findall('"year":(\d+)', enddate_txt)
end_month = re.findall('"month":(\d+)', enddate_txt)
startdate = ''
if start_year:
startdate += '%s' % start_year[0]
if start_month:
startdate += '.%s' % start_month[0]
enddate = ''
if end_year:
enddate += '%s' % end_year[0]
if end_month:
enddate += '.%s' % end_month[0]
if len(startdate) > 0 and len(enddate) == 0:
enddate = '现在'
projectTime += ' 时间: %s ~ %s' % (startdate, enddate)
if title:
print ' %s %s %s' % (title[0], projectTime, ' 项目描述: %s' % description[0] if description else '')
volunteer = re.findall('(\{[^\{]*?profile\.VolunteerExperience"[^\}]*?\})', content)
if volunteer:
print '志愿者经历:'
for one in volunteer:
companyName = re.findall('"companyName":"(.*?)"', one)
role = re.findall('"role":"(.*?)"', one)
timePeriod = re.findall('"timePeriod":"(.*?)"', one)
volunteerTime = ''
if timePeriod:
startdate_txt = ' '.join(re.findall('(\{[^\{]*?"\$id":"%s,startDate"[^\}]*?\})' % timePeriod[0].replace('(', '\(').replace(')', '\)'), content))
enddate_txt = ' '.join(re.findall('(\{[^\{]*?"\$id":"%s,endDate"[^\}]*?\})' % timePeriod[0].replace('(', '\(').replace(')', '\)'), content))
start_year = re.findall('"year":(\d+)', startdate_txt)
start_month = re.findall('"month":(\d+)', startdate_txt)
end_year = re.findall('"year":(\d+)', enddate_txt)
end_month = re.findall('"month":(\d+)', enddate_txt)
startdate = ''
if start_year:
startdate += '%s' % start_year[0]
if start_month:
startdate += '.%s' % start_month[0]
enddate = ''
if end_year:
enddate += '%s' % end_year[0]
if end_month:
enddate += '.%s' % end_month[0]
if len(startdate) > 0 and len(enddate) == 0:
enddate = '现在'
volunteerTime += ' 时间: %s ~ %s' % (startdate, enddate)
if companyName:
print ' %s %s %s' % (companyName[0], volunteerTime, ' 角色: %s' % role[0] if role else '')
print '\n\n'
def crawl(url, s):
""" 抓取每一个搜索结果 """
try:
url = get_linkedin_url(url, copy.deepcopy(s)).replace('cn.linkedin.com', 'www.linkedin.com') # 百度搜索出的结果是百度跳转链接,要提取出linkedin的链接。
if len(url) > 0 and url not in LINKS_FINISHED:
LINKS_FINISHED.append(url)
failure = 0
while failure < 10:
try:
r = s.get(url, timeout=10)
except Exception, e:
failure += 1
continue
if r.status_code == 200:
parse(r.content, url)
break
else:
print '%s %s' % (r.status_code, url)
failure += 2
if failure >= 10:
print 'Failed: %s' % url
except Exception, e:
pass
if __name__ == '__main__':
s = login(laccount='[email protected]', lpassword='abc @123') # 测试账号
company_name = raw_input('Input the company you want to crawl:')
maxpage = 50 # 抓取前50页百度搜索结果,百度搜索最多显示76页
# 百度搜索
url = 'http://www.baidu.com/s?ie=UTF-8&wd=%20%7C%20领英%20' + quote(company_name) + '%20site%3Alinkedin.com'
results = []
failure = 0
while len(url) > 0 and failure < 10:
try:
r = requests.get(url, timeout=10)
except Exception, e:
failure += 1
continue
if r.status_code == 200:
hrefs = list(set(re.findall('"(http://www\.baidu\.com/link\?url=.*?)"', r.content))) # 一页有10个搜索结果
for href in hrefs:
crawl(href, copy.deepcopy(s))
results += hrefs
tree = etree.HTML(r.content)
nextpage_txt = tree.xpath('//div[@id="page"]/a[@class="n" and contains(text(), "下一页")]/@href'.decode('utf8'))
url = 'http://www.baidu.com' + nextpage_txt[0].strip() if nextpage_txt else ''
failure = 0
maxpage -= 1
if maxpage <= 0:
break
else:
failure += 2
print 'search failed: %s' % r.status_code
if failure >= 10:
print 'search failed: %s' % url