油猴插件之指定(招聘)数据获取

插件的作用是辅助我们精准且快速获取信息,提升效率。
使用jquery获取指定数据并清洗,点击按钮下载为csv或存储到后台。

如何使用tampermonkey

看官方教程,推荐使用edge浏览器
https://www.tampermonkey.net/

通过jquery获取指定数据

招聘网站

在插件脚本 ==UserScript== 中配置目标网站

1
2
3
4
5
6
// @match        https://www.zhipin.com/job_detail/*
// @match https://jobs.zhaopin.com/*
// @match https://www.liepin.com/job/*
// @match https://www.lagou.com/wn/jobs/*
// @match https://*.58.com/*
// @match https://jobs.51job.com/*

目标数据

1
const data = ["岗位名称", "薪资下限", "薪资上限", "薪资", "经验要求", "学历要求", "工作地点", "数据来源", "职位说明", "福利", "企业名称", "企业标签", "链接"]

获取数据并清洗

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
const FNS = {
"lagou.com": () => {
let [city, experience, education] = Array.from(
document.querySelectorAll(".job_request h3 span")
)
.map((span) => span.textContent)
.map((s) => s.replace(" /", "").trim());
return {
url: location.href,
source: "拉勾网",
name: document.querySelector(".position-head-wrap-position-name")
?.textContent,
salary: document.querySelector(".salary")?.textContent,
tags: document
.querySelector(".job-advantage p")
.textContent.split(/\s+/),
experience,
education,
info: document.querySelector(".job-detail")?.textContent,
city,
location: document
.querySelector(".work_addr span")
?.textContent?.replace("- ", ""),
company: document.querySelector(".job_company_content .fl-cn")
?.textContent,
company_tags: Array.from(
document.querySelectorAll(".c_feature li h4")
).map((li) => li.textContent),
};
},
...
}

页面插入按钮

我们需要有选择的抓取数据,在页面中插入按钮

1
2
3
4
let downloadLink = document.createElement("div");
downloadLink.innerHTML =
"<a onclick=\"window.postMessage('pluginGetData', '*')\" style=\"display:block;width:300px;height:100px; line-height: 100px; position:fixed; top:10px;right:10px;z-index:999999; background-color:#c4261d; box-shadow: 1px 2px 3px #000; color:#fff; font-size: 28px; text-align:center; cursor: pointer;\">》》获取数据《《</a>";
document.body.insertBefore(downloadLink, document.body.children[0]);

GM_ 与 window不能同时使用,因为GM_*在沙盒中运行。点击按钮后发送postMessage消息

1
window.postMessage('pluginGetData', '*')

页面与沙盒通信

接收页面消息,原页面与沙盒通信

1
2
3
4
5
6
7
8
window.addEventListener("message", receiveMessage, false);

function receiveMessage(event) {
console.log("receiveMessage", event);
if (event.data.includes("pluginGetData")) {
pluginGetData();
}
}

清洗后的数据上报

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
function pluginGetData() {
// 获取数据,清洗中...
// 清洗后的数据
console.log(newJobData);
// 数据上报
GM_xmlhttpRequest({
url: "https://*/parse/classes/RecruitmentData",
method: "POST",
data: JSON.stringify(newJobData),
headers: {
"Content-Type": "application/json;charset=utf-8",
"X-Parse-Application-Id": "*",
},
onload: function (xhr) {
const data = xhr.responseText;
if (data.includes("objectId")) {
alert("操作成功!" + data);
} else {
alert("操作失败,请重试!" + data);
}
},
onerror: function (err) {
alert("操作失败,请重试!" + err);
},
});
}

其它

tips: 也可以直接在页面清洗数据,点击按钮后直接下载

1
2
3
4
5
6
// newJobData 清洗后数据
let downloadLink = document.createElement("a");
const blob = new Blob([newJobData], {type: 'text/csv,charset=UTF-8'});
const uri = URL.createObjectURL(blob);
downloadLink.src = uri
downloadLink.download = (name+".csv")||"temp.csv";

完整代码

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
// ==UserScript==
// @name 招聘数据
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author Csorz
// @match https://www.zhipin.com/job_detail/*
// @match https://jobs.zhaopin.com/*
// @match https://www.liepin.com/job/*
// @match https://www.lagou.com/wn/jobs/*
// @match https://*.58.com/*
// @match https://jobs.51job.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=liepin.com
// @grant GM_xmlhttpRequest
// @grant unsafeWindow
// @require https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js
// ==/UserScript==

(function () {
"use strict";
const pluginGetData = () => {
const SALARY =
/(?<low>[0-9.])(?<lowUnit>[kK千wW万]?)[^0-9.]+(?<high>[0-9.])(?<highUnit>[kK千wW万]?)/;
const SALARY_UNITS = {
k: 1000,
K: 1000,
千: 1000,
w: 10000,
W: 10000,
万: 10000,
};

function parseSalary(salary) {
if (salary) {
const data =
/(?<low>[0-9.]+)(?<lowUnit>[kK千wW万]?)[^0-9.]+(?<high>[0-9.]+)(?<highUnit>[kK千wW万]?)/.exec(
salary
)?.groups;
if (data) {
const { low, lowUnit, high, highUnit } = data;
return {
low: Number(low) * (SALARY_UNITS[lowUnit || highUnit] || 1),
high: Number(high) * (SALARY_UNITS[highUnit] || 1),
};
}
}
return {};
}

const FNS = {
"51job.com": () => {
let [city, experience, education] = document
.querySelector(".ltype")
.textContent.split("|")
.map((s) => s.trim());
return {
url: location.href,
source: "前程无忧",
name: document.querySelector("h1")?.textContent,
salary: document.querySelector("strong")?.textContent,
tags: Array.from(document.querySelectorAll(".jtag span")).map(
(span) => span.textContent
),
experience,
education,
info: document.querySelector(".job_msg")?.textContent,
city,
location: Array.from(document.querySelectorAll(".bmsg .fp"))
.map((p) => p.textContent)
.filter((s) => s.startsWith("上班地址:"))
.map((s) => s.replace("上班地址:", ""))[0],
company: document.querySelector(".com_name")?.textContent,
company_tags: Array.from(document.querySelectorAll(".com_tag p"))
.map((span) => span.title)
.filter((s) => s),
};
},
"58.com": () => {
let [count, education, experience] = Array.from(
document.querySelectorAll(".pos_base_condition span")
).map((span) => span.textContent);
return {
url: location.href,
source: "58同城",
name: document.querySelector(".pos_name")?.textContent,
salary: document.querySelector(".pos_salary")?.textContent,
tags: Array.from(document.querySelectorAll(".pos_welfare span")).map(
(span) => span.textContent
),
experience,
education,
info: document.querySelector(".posDes")?.textContent,
city: document.querySelector(".pos_address .pos_area_item")
?.textContent,
location: document.querySelector(".pos_area_span pos_address > span")
?.textContent,
company: document.querySelector(".comp_baseInfo_title a")
?.textContent,
company_tags: Array.from(
document.querySelectorAll(".company_baseInfo > p")
)
.map((p) => p.textContent)
.filter((s) => s),
};
},
"lagou.com": () => {
let [city, experience, education] = Array.from(
document.querySelectorAll(".job_request h3 span")
)
.map((span) => span.textContent)
.map((s) => s.replace(" /", "").trim());
return {
url: location.href,
source: "拉勾网",
name: document.querySelector(".position-head-wrap-position-name")
?.textContent,
salary: document.querySelector(".salary")?.textContent,
tags: document
.querySelector(".job-advantage p")
.textContent.split(/\s+/),
experience,
education,
info: document.querySelector(".job-detail")?.textContent,
city,
location: document
.querySelector(".work_addr span")
?.textContent?.replace("- ", ""),
company: document.querySelector(".job_company_content .fl-cn")
?.textContent,
company_tags: Array.from(
document.querySelectorAll(".c_feature li h4")
).map((li) => li.textContent),
};
},
"liepin.com": () => {
let [city, experience, education] = Array.from(
document.querySelectorAll(".job-properties span")
)
.map((span) => span.textContent)
.filter((s) => s);
return {
url: location.href,
source: "猎聘",
name: document.querySelector(".name")?.textContent,
salary: document.querySelector(".salary")?.textContent,
tags: Array.from(document.querySelectorAll(".labels span")).map(
(span) => span.textContent
),
experience,
education,
info: document.querySelector("[data-selector='job-intro-content']")
?.textContent,
city,
location: Array.from(
document.querySelectorAll(".company-other .label-box")
)
.filter(
(div) => div.querySelector(".label")?.textContent === "职位地址:"
)
.map((div) => div.querySelector(".text")?.textContent)[0],
company: document.querySelector(".company-card .name")?.textContent,
company_tags: Array.from(
document.querySelectorAll(".company-other .label-box")
)
.filter(
(div) => div.querySelector(".label")?.textContent !== "职位地址:"
)
.map((div) => div.querySelector(".text")?.textContent),
};
},
"zhaopin.com": () => {
let [city, experience, education] = Array.from(
document.querySelectorAll(".summary-plane__info li")
).map((li) => li.textContent);
return {
url: location.href,
source: "智联招聘",
name: document.querySelector(".summary-plane__title")?.textContent,
salary: document.querySelector(".summary-plane__salary")?.textContent,
tags: Array.from(
document.querySelectorAll(".highlights__content span")
).map((span) => span.textContent),
experience,
education,
info: document.querySelector(".describtion")?.textContent,
city,
location: document.querySelector(".job-address__content-text")
?.textContent,
company: document.querySelector(".company a")?.textContent,
company_tags: Array.from(
document.querySelectorAll(".company__detail button")
).map((button) => button.textContent),
};
},
"zhipin.com": () => {
let [city, experience, education] = Array.from(
document.querySelector(".job-primary .info-primary > p")
?.childNodes || []
)
.map((span) => span.textContent)
.filter((s) => s);
return {
url: location.href,
source: "BOSS直聘",
name: document.querySelector(".job-primary .name h1")?.textContent,
salary: document.querySelector(".job-primary .salary")?.textContent,
tags: Array.from(
document.querySelectorAll(
".job-primary > .tag-container-new > .job-tags span"
)
).map((span) => span.textContent),
experience,
education,
info: document.querySelector(".job-sec .text")?.textContent,
city,
location: document.querySelector(".location-address")?.textContent,
company: document
.querySelector(".company-info [ka='job-detail-company_custompage']")
?.textContent?.trim(),
company_tags: Array.from(
document.querySelectorAll(".sider-company > p:not(.title)")
).map((p) => p.textContent),
};
},
};

let [domain] = /[^.]+.[^.]+$/.exec(location.hostname);
const fns = FNS[domain];
const job = fns();
const {
name,
salary,
experience,
education,
city,
source,
info,
tags,
company,
company_tags,
url,
} = job;
const { low, high } = parseSalary(salary);
const newJobData = {
name,
salary_low: low,
salary_high: high,
salary,
experience,
education,
city,
source,
info,
tags: tags?.join(","),
company,
company_tags: company_tags?.join(","),
url,
};
// 数据说明
// const data = [
// ["岗位名称", "薪资下限", "薪资上限", "薪资", "经验要求", "学历要求", "工作地点", "数据来源", "职位说明", "福利", "企业名称", "企业标签", "链接"]
// ];


console.log(newJobData);
GM_xmlhttpRequest({
url: "https://*/parse/classes/RecruitmentData",
method: "POST",
data: JSON.stringify(newJobData),
headers: {
"Content-Type": "application/json;charset=utf-8",
"X-Parse-Application-Id": "*",
},
onload: function (xhr) {
const data = xhr.responseText;
if (data.includes("objectId")) {
alert("操作成功!" + data);
} else {
alert("操作失败,请重试!" + data);
}
},
onerror: function (err) {
alert("操作失败,请重试!" + err);
},
});
};

// 接收页面消息,原页面与沙盒通信
window.addEventListener("message", receiveMessage, false);

function receiveMessage(event) {
console.log("receiveMessage", event);
if (event.data.includes("pluginGetData")) {
pluginGetData();
}
}

// 页面插入按钮
let downloadLink = document.createElement("div");
// GM_ 与 window不能同时使用,因为GM_*在沙盒中运行。
downloadLink.innerHTML =
"<a onclick=\"window.postMessage('pluginGetData', '*')\" style=\"display:block;width:300px;height:100px; line-height: 100px; position:fixed; top:10px;right:10px;z-index:999999; background-color:#c4261d; box-shadow: 1px 2px 3px #000; color:#fff; font-size: 28px; text-align:center; cursor: pointer;\">》》获取数据《《</a>";

// 可以点击下载csv文件
// const blob = new Blob([newJobData], {type: 'text/csv,charset=UTF-8'});
// const uri = URL.createObjectURL(blob);
// downloadLink.src = uri
// downloadLink.download = (name+".csv")||"temp.csv";

document.body.insertBefore(downloadLink, document.body.children[0]);

})();


油猴插件之指定(招聘)数据获取
http://example.com/20230226-油猴插件之指定(招聘)数据获取/
作者
csorz
发布于
2023年2月26日
许可协议