-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathboatTicket.js
186 lines (175 loc) · 5.16 KB
/
boatTicket.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
/**
* 船票爬虫 踏破铁鞋网http://www.tapotiexie.com
*/
var request = require("request");
var cheerio = require("cheerio");
var async = require("async");
var mysql = require("mysql");
var dbUtil = require("./dbUtil");
var schedule = require("node-schedule"); // 定时任务
function get() {
var domain = "http://www.tapotiexie.com";
var url = "http://www.tapotiexie.com/Line/index/name/yt/p/";
var txtSource = "踏破铁鞋"; //文本来源
var startPage = 1; // 开始页
var endPage = 3; // 结束页
var url_list = [];
var list = [];
// 确定爬取的Url队列
for (var i = startPage; i <= endPage; i++) {
url_list.push(url + i + ".html");
}
async.eachSeries(
url_list,
function (arr_url, callback) {
console.log("正在抓取" + arr_url + "的数据...");
request(arr_url, function (err, data) {
if (err) {
return console.error(err);
}
// 通过cheerio的load方法解析整个文档,就是html页面所有内容,可以通过console.log($.html());在控制台查看
var $ = cheerio.load(data.body.toString());
$(".tptx_ytgt .tptx_ytgt_4").each(function () {
var $me = $(this);
//解析船公司和船字段
var arr1 = analyStr($me.find(".tptx_ytgt_2b a").text());
//解析晚和天
var arr2 = analyDay($me.find(".tptx_jcyj_2ab_1 span").text());
var item = {
txtCompany: arr1[0], //公司
txtCruise: arr1[1], //游轮
txtLine: analyLine(
$me.find(".tptx_jcyj_2ab_1 ul li:first-child").text()
), // 航线
txtStartDate: analyStart(
$me.find(".tptx_jcyj_2ab_1 li").eq(1).text(),
$me.find(".tptx_jcyj_2ab_1 span").text()
),
numDay: Number(arr2[1]), // 天数
numNight: Number(arr2[0]), //夜数
numPrice: analyPrice(
$me.find(".tptx_jcyj_2ac .tptx_jcyj_2ac_1").text()
), // 价格
txtUrl: domain + $me.find(".tptx_ytgt_2b a").attr("href"), //详情url
};
list.push(item);
});
callback(err, list);
});
},
function (err) {
if (err) {
return console.error(err.stack);
}
/*写入数据库*/
async.eachSeries(
list,
function (record, callback) {
console.log("正在写入" + record.txtStartDate + "的数据...");
var str = "";
for (var i in record) {
str += "'" + record[i] + "',";
}
str = str.substr(0, str.length - 1);
console.log(str);
// 拼接sql
var sql =
"INSERT INTO tb_crawl_boat_ticket (company,cruise,line,state_date,boat_day,night,price,url)values(" +
str +
")";
dbUtil
.EXECUTE(sql)
.then((res) => {
console.log("res==>", res);
// 受影响的行数
if (res.affectedRows == 1) {
callback(err);
}
})
.catch((data) => {
console.log("data==>", data);
});
},
function (err) {
if (err) {
return console.error(err.stack);
}
console.log("处理成功!");
}
);
}
);
}
/*---------字段解析start----------*/
/*按中间的空格解析字符串*/
function analyStr(str) {
var _newStr = "";
for (i in str) {
if (str[i].trim() == "") {
_newStr += "|";
} else {
_newStr += str[i];
}
}
return _newStr.split("||");
}
/*解析航线*/
function analyLine(str) {
var arr1 = str.split("邮轮线路:");
return arr1[1];
}
/*解析价格*/
function analyPrice(str) {
if (str == "售罄") {
return 0;
} else {
var arr1 = str.split("¥");
var arr2 = arr1[1].split("起");
return Number(arr2[0]);
}
}
/*解析晚和天*/
function analyDay(str) {
var arr1 = str.split("晚");
var arr2 = arr1[1].split("天");
var _newStr = arr1[0] + "|" + arr2[0];
return _newStr.split("|");
}
/*解析出发日期*/
function analyStart(str1, str2) {
var arr1 = str1.split(str2);
var arr2 = arr1[0].split("出发时间:");
return arr2[1];
}
/*---------字段解析end----------*/
/*执行主程序*/
get();
/*定时任务*/
// var registTask = function (hour, minute, taskname, fn) {
// var rule = new schedule.RecurrenceRule();
// //每天这个时刻定时执行任务
// rule.dayOfWeek = [0, new schedule.Range(1, 6)];
// rule.hour = hour;
// rule.minute = minute;
// var j = schedule.scheduleJob(rule, function () {
// console.log("开始爬取:" + taskname); fn && fn();
// });
// }
// registTask(20, 51, 'boatTicket', function () {
// get();
// });
// registTask();
/*处理乱码*/
function reconvert(str) {
str = str.replace(/(\\u)(\w{4})/gi, function ($0) {
return String.fromCharCode(
parseInt(escape($0).replace(/(%5Cu)(\w{4})/g, "$2"), 16)
);
});
str = str.replace(/(&#x)(\w{4});/gi, function ($0) {
return String.fromCharCode(
parseInt(escape($0).replace(/(%26%23x)(\w{4})(%3B)/g, "$2"), 16)
);
});
return str;
}