-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrawler_163.js
213 lines (207 loc) · 7.84 KB
/
crawler_163.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
var crawler_request = require('request');
var crawler_iconv = require('iconv-lite');
var crawler_cheerio = require('cheerio');
require('date-utils');
// 连接数据库
var crawler_sql = require("./crawler_sql.js");
// var crawler_sql = require("mysql");
// var pool = crawler_sql.createPool({
// host: '127.0.0.1',
// user: 'root',
// password: 'root',
// database: 'crawl'
// });
// var query = function(sql, sqlparam, callback) {
// pool.getConnection(function(err, conn) {
// if (err) {
// callback(err, null, null);
// } else {
// conn.query(sql, sqlparam, function(qerr, vals, fields) {
// conn.release(); //释放连接
// callback(qerr, vals, fields); //事件驱动回调
// });
// }
// });
// };
// var query_noparam = function(sql, callback) {
// pool.getConnection(function(err, conn) {
// if (err) {
// callback(err, null, null);
// } else {
// conn.query(sql, function(qerr, vals, fields) {
// conn.release(); //释放连接
// callback(qerr, vals, fields); //事件驱动回调
// });
// }
// });
// };
// exports.query = query;
// exports.query_noparam = query_noparam;
// 爬取网页首页
function request(url, callback) {
var options = {
url: url,
encoding: null,
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36'
},
timeout: 10000
}
crawler_request(options, callback);
};
var crawler_url = 'https://news.163.com/';
crawler();
function crawler() {
request(crawler_url, function(err, res, body) {
// 网页解析
try {
// 编码转换
var url_encoding = 'UTF-8';
var url_html = crawler_iconv.decode(body, url_encoding);
//解析网页
var $ = crawler_cheerio.load(url_html, { decodeEntities: true });
} catch (e) {
console.log('页面解码错误:' + e);
}
// 判断网页是否存在超链接
var url_hrefs;
try {
url_hrefs = eval("$('a')");
} catch (e) {
console.log('页面不存在超链接' + e);
}
// 遍历网页中所有超链接
url_hrefs.each(function(i, e) {
// 获取新闻
var news_url = "";
try {
var url_href = "";
url_href = $(e).attr("href");
if (typeof(url_href) == "undefined") {
return true;
}
if (url_href.toLowerCase().indexOf('http://') >= 0 || url_href.toLowerCase().indexOf('https://') >= 0) {
news_url = url_href;
} else if (url_href.startsWith('//')) {
news_url = 'https:' + url_href;
} else {
news_url = crawler_url.substr(0, crawler_url.lastIndexOf('/') + 1) + url_href;
}
} catch (e) {
console.log('获取新闻页面出错' + e);
}
// 检验新闻网页url是否符合url命名格式
var news_reg = /\/news\/article\/([a-zA-Z0-9]{16}).html/;
var news_reg_special = /\/news\/article\/([a-zA-Z0-9]{8})0001982T.html/;
// 如:https://www.163.com/news/article/G8HQOAKE0001899O.html
if (!news_reg.test(news_url) || news_reg_special.test(news_url)) {
console.log('新闻链接不符合格式!');
return;
}
// 爬取新闻页面
var news_search_sql = 'select url from news where url=?';
var news_search = [news_url];
crawler_sql.query(news_search_sql, news_search, function(qerr, vals, fields) {
if (vals.length > 0) {
console.log('该新闻页面已被爬取!')
} else {
crawler_news_url(news_url);
}
});
});
});
}
// 爬取新闻链接
function crawler_news_url(news_url) {
request(news_url, function(err, res, body) {
// 网页解析
try {
// 编码转换
var url_encoding = 'UTF-8';
var url_html = crawler_iconv.decode(body, url_encoding);
//解析网页
var $ = crawler_cheerio.load(url_html, { decodeEntities: true });
} catch (e) {
console.log('页面解码错误:' + e);
}
// 定义新闻信息json
var news = {};
news.crawler_time = (new Date()).toFormat("YYYY-MM-DD HH:MM:SS.SSSS");
news.url = news_url;
news.url_encoding = 'UTF-8';
news.keywords = '';
news.title = '';
news.date = new Date();
news.author = '';
news.source = '';
news.summary = '';
news.content = '';
// 获取新闻关键词
try {
news.keywords = eval("$('meta[name=\"keywords\"]').eq(0).attr(\"content\")");
} catch (e) {
console.log('新闻关键词获取错误:' + e);
}
// 获取新闻标题
try {
news.title = eval("$('title').text()").replace(/[\r\n\s]/g, "");
} catch (e) {
console.log('新闻标题获取错误:' + e);
}
// 获取新闻时间
try {
news.date = eval("$('#ne_wrap').eq(0).attr(\"data-publishtime\")");
} catch (e) {
console.log('新闻日期获取错误:' + e);
}
// 获取新闻作者
try {
news.author = eval("$('.icon').eq(0).attr(\"alt\")");
if (news.author == 'netease') {
news.author = eval("$('.post_author').text()").replace(/[\r\n\s]/g, "").replace("本文来源:", "");
var author_reg = /责任编辑:.+_/;
news.author = author_reg.exec(news.author).toString().replace("责任编辑:", "").replace("_", "");
}
} catch (e) {
console.log('新闻作者获取错误:' + e);
}
// 获取新闻来源
try {
news.source = eval("$('.post_info').children(':first').text()").replace(/[\r\n\s]/g, "");
if (news.source == '举报') {
news.source = eval("$('.post_info').prop('firstChild').nodeValue").replace(/[\r\n\s]/g, "");
var source_reg = /.+来源:/;
var tmp = source_reg.exec(news.source).toString();
news.source = news.source.replace(tmp, "");
}
} catch (e) {
console.log('新闻来源获取错误:' + e);
}
// 获取新闻摘要
try {
news.summary = eval("$('meta[name=\"description\"]').eq(0).attr(\"content\")").replace(/[\r\n\s]/g, "");
} catch (e) {
console.log('新闻摘要获取错误:' + e);
}
// 获取新闻内容
try {
news.content = eval("$('.post_body').text()").replace(/[\r\n\s]/g, "");
} catch (e) {
console.log('新闻内容获取错误:' + e);
}
console.log(JSON.stringify(news));
// 写入数据库
if (news.content != '') {
var news_add_sql = 'INSERT INTO news(url, source, url_encoding, title, keywords, author, date, crawler_time, summary, content) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
var news_add = [news.url, news.source, news.url_encoding,
news.title, news.keywords, news.author, news.date,
news.crawler_time, news.summary, news.content
];
crawler_sql.query(news_add_sql, news_add, function(qerr, vals, fields) {
if (qerr) {
console.log(qerr);
}
});
}
});
}