-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmvdis_crawler.js
260 lines (254 loc) · 10 KB
/
mvdis_crawler.js
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
/**
*這個檔案主要處理爬取網頁後擷取我要的結果,所以這整段看起來都很怪,也很需要持續的改善
TODO: 以文件寫清楚目前監理站網頁的邏輯,在寫清楚我自己爬蟲的邏輯
*/
const axios = require("axios");
const cheerio = require("cheerio");
const category = require("./session_category");
const { write_file } = require("./utils/helper.js");
const dmvNo_list = require("./dmvNo.json");
const mvdis_base_url = "https://www.mvdis.gov.tw/m3-emv-trn/exm/";
module.exports = {
/* 爬出該地點該時間可以報考的名額 */
locations_query: async (licenseTypeCode, expectExamDateStr, dmvNo) => {
try {
const response = await axios({
method: "post",
url: `${mvdis_base_url}locations`,
data: {
method: "query",
licenseTypeCode: licenseTypeCode,
expectExamDateStr: expectExamDateStr,
// 註解是不需要的引數
// 目前我看大部分監理站也只有開平日,就算有週末,使用者也能擴大搜尋範圍找到周末場次
// '_onlyWeekend':'on',
// 'onlyWeekend':'true',
// 'dmvNoLv1':'40',
dmvNo: dmvNo,
},
headers: {
"Content-Type": "multipart/form-data",
},
});
const $ = cheerio.load(response["data"]);
// 網站上的錯誤提示會顯示在這
const headerMessage = $("#headerMessage");
if (
headerMessage.text() ===
"該條件查詢不到符合的考試場次,敬請重新選擇"
) {
console.log(headerMessage.text());
console.log("出事了阿伯");
return false;
}
const result = [];
const search_table = $("#trnTable tbody td");
for (let i = 0; i < search_table.length / 4; i++) {
// 走訪
const t = i * 4;
const date = search_table
.eq(t)
.text()
.replace(/\t/g, "")
.trim();
const description = search_table
.eq(t + 1)
.text()
.replace(/\t/g, "")
.trim();
let number = search_table
.eq(t + 2)
.text()
.replace(/\t/g, "")
.trim();
if (number === "額滿") {
number = 0;
// 沒有名額就不能且不用取得secId, divId, expectExamDateStr
result.push(Object.assign({ date, description, number }));
} else {
// 剩餘名額應該要是數字類型
number = parseInt(number);
// 有名額才能取得secId, divId, expectExamDateStr,用正則表達式找出
const _temp = search_table
.eq(t + 3)
.html()
.replace(/[\t\n]/g, "")
.match(/preAdd\(.{22,23}\)/);
const _temp_list = _temp[0].split("'");
// TODO: 解決這裡expectExamDateStr的重名問題
const expectExamDateStr = _temp_list[1];
const secId = _temp_list[3];
const divId = _temp_list[5];
result.push(
Object.assign({
date,
description,
number,
expectExamDateStr,
secId,
divId,
})
);
}
}
console.table(result);
// 蒐集各監理站資訊可以先從這裡處理
// 存檔案
const file_name = `./result/${licenseTypeCode}_${dmvNo}_${expectExamDateStr}.json`;
const dmvName = dmvNo_list[dmvNo];
const category_result = category(result, dmvName, licenseTypeCode);
write_file(file_name, JSON.stringify(category_result, null, 4));
return result;
} catch (error) {
console.error(error);
return false;
}
},
/* 拿個人資料報名該場次 */
sign_up: async (
licenseTypeCode,
secDateStr,
dmvNo,
secId,
divId,
idNo,
birthdayStr,
name,
contactTel,
email
) => {
try {
const response = await axios({
method: "post",
url: `${mvdis_base_url}signUp`,
data: {
method: "add",
licenseTypeCode: licenseTypeCode,
secDateStr: secDateStr,
dmvNo: dmvNo,
secId: secId,
divId: divId,
idNo: idNo,
birthdayStr: birthdayStr,
name: name,
contactTel: contactTel,
email: email,
},
headers: {
"Content-Type": "multipart/form-data",
},
});
const $ = cheerio.load(response["data"]);
// 一些錯誤提示會在這邊顯示,但是像是身分證不符合格式就會顯示在網頁表單上,可能要額外檢查才能找出哪裡填錯
const headerMessage = $("#headerMessage");
console.log(headerMessage.text());
// 在回傳的HTML的<script>裡直接尋找 報名成功 4個字就可以確認是否報名成功了
if (/報名成功/.test($("script").text())) {
return true;
} else {
return false;
}
} catch (error) {
console.error(error);
return false;
}
},
/* 拿reservationPK取消報名該場次 */
query_query: async (idNo, birthdayStr) => {
try {
const response = await axios({
method: "post",
url: `${mvdis_base_url}query`,
data: {
method: "query",
idNo: idNo,
birthdayStr: birthdayStr,
},
headers: {
"Content-Type": "multipart/form-data",
},
});
const $ = cheerio.load(response["data"]);
// 一些錯誤提示會在這邊顯示
const headerMessage = $("#headerMessage");
if (headerMessage.text() === "已查無報名資料,可「新增報名」") {
console.log(headerMessage.text());
return {
result: {},
reservationPK: "",
message: headerMessage.text(),
};
} else {
// 這裡只擷取第一筆報名結果,但如果這個人第一次沒考過,第二次再考會怎麼辦?
// 會得到在網頁上查詢報名的結果
// TODO: 找人討論這裡該拿第一筆還是每一筆,先拿第一筆並分成四個資料
const table_elem = $(".tb_list_std tbody tr td");
const place_of_test = table_elem.eq(0).text();
const type_of_test = table_elem.eq(1).text();
const date_of_test = table_elem
.eq(2)
.text()
.replace(/\t/g, "")
.trim();
const desc = table_elem.eq(3).text().replace(/\t/g, "").trim();
const reservationPK = $(".tb_list_std tbody tr")
.find("a")
.last()
.attr("onclick")
.split("'")[1];
console.log(place_of_test);
console.log(type_of_test);
console.log(date_of_test);
console.log(desc);
console.log(reservationPK);
return {
result: {
place_of_test: place_of_test,
type_of_test: type_of_test,
date_of_test: date_of_test,
desc: desc,
},
reservationPK: reservationPK,
};
}
} catch (error) {
console.error(error);
return false;
}
},
/* 拿reservationPK取消報名該場次 */
query_cancel: async (reservationPK) => {
try {
const response = await axios({
method: "post",
url: `${mvdis_base_url}query`,
data: {
method: "cancel",
reservationPK: reservationPK,
},
headers: {
"Content-Type": "multipart/form-data",
},
});
const $ = cheerio.load(response["data"]);
// 網站上的錯誤提示會顯示在這
const headerMessage = $("#headerMessage");
const result = headerMessage.text();
if (result === "請輸入查詢條件") {
console.log("取消報名成功");
return true;
} else if (result === "「取消報名」失敗,請洽所報名之監理所站") {
console.log(result);
return false;
} else if (/取消報名成功/.test($("script").text())) {
// 在回傳的HTML的<script>裡直接尋找 取消報名成功 4個字就可以確認是否報名成功了
return true;
} else {
throw new Error("出事了阿伯!");
}
} catch (error) {
console.error(error);
return false;
}
},
};