forked from ls1248659692/python_guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlagou_data_visualization.py
More file actions
344 lines (274 loc) · 11 KB
/
lagou_data_visualization.py
File metadata and controls
344 lines (274 loc) · 11 KB
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
# coding:utf-8
# !/usr/bin/env python
import json
import os
import re
import time
import pickle
import traceback
from multiprocessing import Pool
from compiler.ast import flatten
import jieba
import requests
from pyecharts import Geo
from scipy.misc import imread
from wordcloud import WordCloud
from report_util.data_analysis_head import *
from constant import LagouDataVisualization as LAGOU
PROJECT_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
filedir = os.path.join(PROJECT_ROOT, LAGOU.FILE_DOWNLOAD_PATH)
filepath = os.path.join(filedir, 'lagou_data_visualization.csv')
def save_cookies():
response = requests.get(
LAGOU.COOKIES_URL, headers=LAGOU.COOKIES_HEADER, timeout=10
)
if response.status_code == 200:
cookies_dict = requests.utils.dict_from_cookiejar(response.cookies)
with open('cookies', 'w') as f:
pickle.dump(cookies_dict, f)
return response.cookies
else:
print LAGOU.ERROR_MSG.format(status_code=response.status_code)
def get_cookies():
with open('cookies', 'r') as f:
cookies = requests.utils.cookiejar_from_dict(pickle.load(f))
return cookies
def get_job_max_pageno(needAddtionalResult='false', keyword='python'):
form = {
'first': 'true',
'kd': keyword,
'pn': '1'
}
response = requests.post(
LAGOU.JOB_URL.format(need=needAddtionalResult),
data=form,
headers=LAGOU.Job_HEADER,
# cookies=save_cookies(),
timeout=5
)
if response.status_code == 200:
try:
total_count = json.loads(response.text)['content']['positionResult']['totalCount']
max_page_no = int(int(total_count) / 15)
print LAGOU.MAX_PAGENO.format(keyword=keyword, max_page_no=max_page_no, total_count=total_count)
return max_page_no
except:
traceback.print_exc()
return False
else:
print LAGOU.ERROR_MSG.format(status_code=response.status_code)
def spider_lagou_job_data(page_num, needAddtionalResult='false', keyword='python'):
form = {
'first': 'true',
'kd': keyword,
'pn': str(page_num)
}
response = requests.post(
LAGOU.JOB_URL.format(need=needAddtionalResult),
data=form,
headers=LAGOU.Job_HEADER,
# cookies=save_cookies(),
timeout=5
)
if response.status_code == 200:
try:
data_list = json.loads(response.text)['content']['positionResult']['result']
with open(os.path.join(filedir, 'lagou_job_data.txt'), "a+") as f:
for data in data_list:
f.writelines('%s\n' % data)
print LAGOU.OK_MSG.format(n=page_num)
except:
traceback.print_exc()
else:
print LAGOU.ERROR_MSG.format(status_code=response.status_code)
time.sleep(LAGOU.WAIT_TIME)
def multiprocess_spider_job_data(needAddtionalResult='false', keyword='python'):
max_page_no = get_job_max_pageno(needAddtionalResult, keyword)
print('multiprocess spider start.')
pool = Pool(processes=5)
pool.map(spider_lagou_job_data, range(1, max_page_no))
pool.close()
pool.join()
print('multiprocess spider end.')
def save_job_data():
with open(os.path.join(filedir, 'lagou_job_data.txt'), 'r') as f:
data_list = f.readlines()
dict_list = []
for data in data_list:
try:
data = eval(data.strip())
dict_list.append(data)
except:
continue
df = pd.DataFrame(dict_list)
show_cols = ['companyId', 'companyFullName', 'companyShortName', 'companyLabelList',
'city', 'companySize', 'education', 'financeStage', 'businessZones', 'salary',
'firstType', 'secondType', 'thirdType', 'skillLables', 'stationname', 'workYear']
dataset = df[show_cols].fillna('')
cols_join = 'companyLabelList businessZones skillLables'.split()
cols_replace = 'firstType secondType thirdType'.split()
dataset['companyId'] = dataset['companyId'].apply(lambda xx: int(float(xx)) if xx else 0)
dataset[cols_join] = dataset[cols_join].applymap(lambda xx: ','.join(xx) if xx else '')
dataset[cols_replace] = dataset[cols_replace].applymap(lambda xx: xx.replace('|', ',') if xx else '')
print dataset.head()
print dataset.tail()
dataset.to_csv(filepath, encoding='utf-8', index=False)
def spider_lagou_company(havemark=0):
## havemark: 0 for not showing interviewees' remark;
# 1 for showing interviewees' remark;
COMPANY_LIST = list()
for n in range(20):
form = {
'first': 'false',
'pn': str(n),
'sortField': '0',
'havemark': str(havemark)
}
response = requests.post(
LAGOU.COMPANY_URL.format(havemark=havemark),
data=form,
headers=LAGOU.COMPANY_HEADER,
# cookies=save_cookies(),
timeout=5
)
if response.status_code == 200:
try:
company_list_per_page = response.json()['result']
for company in company_list_per_page:
COMPANY_LIST.append([company['companyId'], company['companyShortName'],
company['city'], company['companyFeatures'],
company['companyFullName'], company['financeStage'], company['industryField'],
company['interviewRemarkNum'], company['positionNum'], company['processRate']])
print LAGOU.OK_MSG.format(n=n + 1)
except:
traceback.print_exc()
else:
print LAGOU.ERROR_MSG.format(status_code=response.status_code)
time.sleep(LAGOU.WAIT_TIME)
cols_to_show = ['companyId', 'companyShortName', 'city', 'companyFeatures', 'companyFullName',
'financeStage', 'industryField', 'interviewRemarkNum', 'positionNum', 'processRate']
dataset = pd.DataFrame(COMPANY_LIST, columns=cols_to_show)
dataset.to_csv(os.path.join(filedir, 'lagou_company.csv'), encoding='gbk', index=False)
def data_visualization():
data = pd.read_csv(filepath, encoding='utf-8')
data['salary'] = data['salary'].apply(lambda xx: str(xx).split('-')[0])
edu_dataset = data['education'].value_counts()
wrk_dataset = data['workYear'].value_counts()
cpy_dataset = data['companySize'].value_counts()
salary_dataset = data['salary'].value_counts()[:10]
plot_bar_chart(
edu_dataset,
[u"不限", u"大专", u"本科", u"硕士"],
u'教育背景需求分析',
u"教育背景",
u"数量/个"
)
plot_bar_chart(
wrk_dataset,
[u"不限", u"应届毕业生", u"1年以下", u"1-3年", u"3-5年", u"5-10年"],
u'工作年限需求分析',
u"工作年限",
u"数量/个"
)
plot_bar_chart(
cpy_dataset,
[u"少于15人", u"15-50人", u"50-150人", u"150-500人", u"500-2000人", u"2000人以上"],
u'公司体量需求分析',
u"公司体量",
u"数量/个"
)
plot_bar_chart(
salary_dataset,
[u"5k", u"6k", u"8k", u"10k", u"12k", u"15k", u"18k", u"20k", u"25k", u"30k"],
u'薪资水平需求分析',
u"薪资水平",
u"数量/个"
)
def plot_bar_chart(dataset, order, title, xlable, ylable):
fig, ax = plt.subplots(figsize=(14, 10))
sns.barplot(x=dataset.index, y=dataset.values, palette="deep", order=order)
ax.set_title(title, fontsize=18, position=(0.5, 1.05))
ax.set_xlabel(xlable, fontsize=12)
ax.set_ylabel(ylable, fontsize=12)
ax.tick_params(axis='y', labelsize=10)
ax.tick_params(axis='x', labelsize=10)
ax.set_xticklabels(ax.get_xticklabels(), rotation=-45)
for rect in ax.patches:
y_value = rect.get_height()
x_value = rect.get_x() + rect.get_width() / 2
plt.annotate(
y_value,
(x_value, y_value),
xytext=(0, 5) if y_value > 0 else (0, -5),
textcoords="offset points",
ha='center',
va='bottom' if y_value > 0 else 'top'
)
plt.savefig(filedir + u'{}.png'.format(title), dpi=80, bbox_inches='tight')
def word_segmentation():
data = pd.read_csv(filepath, encoding='utf-8').fillna('')
image = imread(os.path.join(PROJECT_ROOT, LAGOU.BACKGROUND))
df = pd.read_csv(
os.path.join(PROJECT_ROOT, LAGOU.STOPWORDS),
encoding='utf8',
index_col=False
)
jieba.load_userdict(os.path.join(PROJECT_ROOT, LAGOU.USERDICT)) ## 添加多个词汇
stopwords = list(df['stopword'].unique())
stopwords.extend([u'技术', u'高端', u'职位', u'企业', ',', '工程师', '类', '实施', 'IT']) ## 添加停止词
## 需求岗位与技能词汇
word_list = flatten([list(jieba.cut(row['firstType']))
for _, row in data.iterrows()])
word_list.extend(flatten([list(jieba.cut(row['secondType']))
for _, row in data.iterrows()]))
word_list.extend(flatten([list(jieba.cut(row['thirdType']))
for _, row in data.iterrows()]))
word_list = [word for word in word_list if word not in stopwords]
words_count = pd.value_counts(word_list).to_dict()
print pd.value_counts(word_list)
wordcloud = WordCloud(
background_color="white",
mask=image,
font_path=LAGOU.TTF,
max_words=5000,
scale=1.5
)
wordcloud = wordcloud.fit_words(words_count)
plt.figure(
figsize=(10, 6),
dpi=100
)
plt.axis("off")
wordcloud.to_file(filedir + 'data_wordcloud.png')
plt.imshow(wordcloud)
plt.show()
plt.close()
def geographical_location_distribution():
data = pd.read_csv(filepath, encoding='gbk')
data['salary'] = data['salary'].apply(lambda xx: re.sub(u'k|K|以上', '', xx))
data['min_salary'] = data['salary'].apply(lambda xx: float(xx.split('-')[0]) * 1000)
data['max_salary'] = data['salary'].apply(
lambda xx: float(xx.split('-')[1]) * 1000 if len(xx.split('-')) > 1 else float(xx) * 1000)
# print data[data['min_salary'] == data['max_salary']] ## xx以上
dataset = [(city, min_salary) for city, min_salary in data['city min_salary'.split()].values]
geo = Geo("Python Job Distribution", "", title_pos="center", width=2000, height=1200, background_color='#404a59')
attr, value = geo.cast(dataset)
geo.add("", attr, value, type="effectScatter",
is_visualmap=True, maptype='china', visual_range=[0, 300],
effect_scale=5, symbol_size=5, visual_text_color="#fff")
geo.render(filedir + 'job_distribution.html')
# geo.render(path_sign + 'snapshot.png')
def main():
# save_cookies()
# get_cookies()
# get_job_max_pageno()
# spider_lagou_job_data()
# multiprocess_spider_job_data()
# save_job_data()
# spider_lagou_company()
data_visualization()
# word_segmentation()
# geographical_location_distribution()
pass
if __name__ == "__main__":
main()