-
Notifications
You must be signed in to change notification settings - Fork 0
/
book3.js
217 lines (161 loc) · 5.33 KB
/
book3.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
'use strict';
const http = require('http');
const mysql = require('mysql');
const cheerio = require('cheerio');
const iconv = require('iconv-lite');
const async = require('async');
//定义mysql连接选项
const mysqlOpt = {
host: 'localhost',
user: 'root',
password: '123456',
database: 'book'
};
const connection = mysql.createConnection(mysqlOpt);
//目录列表地址
var catelogURL = [];
//列表地址
var listURL = [];
var baseListURL='http://zhannei.baidu.com/cse/search?p=75&s=16881955614253274760&entry=1';
listURL = Array.from({ length: 75 }, (v, k) => {
return '/cse/search?p='+k+'&s=16881955614253274760&entry=1';
});
//console.log(listURL);
//详情地址
var detailURL = [];
//从列表页获取详情页链接
var getDetailURL = (complete) => {
async.eachLimit(listURL, 3, function(item, callback) {
var listOpt = {
host: 'zhannei.baidu.com',
method: 'GET',
path:item,
headers: {
'Referer': 'http://www.pdfshu.org/',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2824.0 Safari/537.36'
}
};
var _DATA = '';
var req = http.request(listOpt, (res) => {
res.setEncoding('utf8');
res.on('data', (chunk) => {
_DATA += chunk;
});
res.on('end', (res) => {
//console.log(_DATA);
var $ = cheerio.load(_DATA);
//填充结果
$('#results .c-title a').each(function(){
detailURL.push($(this).attr('href'));
})
console.log('列表页结束');
//console.log(detailURL.length);
//所有循环结束后调用callback
callback();
});
});
req.on('error', (e) => {
console.log(`报错: ${e.message}`);
callback();
});
//关闭http连接
req.end();
},(err) => {
if (err) {
console.log('获取列表页内容发生了错误:', err);
complete&&complete();
} else {
console.log('获取列表页内容结束');
complete&&complete();
}
});
};
//建立mysql连接
connection.connect();
//从详情页获取具体内容
var getDetailContents = (complete) => {
async.eachLimit(detailURL, 3, function(item, callback) {
console.log('详情页ing'+item);
var detailOpt = {
host: 'www.pdfshu.org',
method: 'GET',
headers: {
'Referer': 'http://www.pdfshu.org/',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2824.0 Safari/537.36'
}
};
detailOpt.path = item.substr(21);
var _DATA =[];
var SQLData=[];
var req = http.request(detailOpt, (res) => {
//res.setEncoding(null);
res.on('data', (chunk) => {
_DATA.push(chunk);
});
res.on('end', (res) => {
console.log('详情页ing');
_DATA=_DATA.toString('utf-8');
var $=cheerio.load(_DATA,{decodeEntities: false});
//console.log(_DATA)
try{
var score=$('.vote-num').text().match(/(\d+)/g)[0];
var title=$('.oh td').eq(0).find('a').last().text();
var thumb=$('.oh td img').eq(0).attr('src');
var format='PDF';
var publicInfo=$('.oh tr').eq(2).find('td').html();
var description=$('.main .mt40').eq(1).find('div').html();
var catalog=$('.main .mt40').eq(2).find('div').html();
/*
console.log('score:-----------',score);
console.log('title:-----------',title);
console.log('thumb:-----------',thumb);
console.log('description:-----------',description);
console.log('catalog:-----------',catalog);
*/
SQLData.push(title,publicInfo,description,format,thumb,catalog,score,'http://www.pdfshu.org/');
console.log('一个详情页请求结束');
//所有循环结束后调用callback
storeData(SQLData,function(){
callback();
})
}catch (err)
{
callback()
}
});
});
req.on('error', (e) => {
console.log(`报错: ${e.message}`);
callback();
});
//关闭http连接
req.end();
}, (err) => {
if (err) {
console.log('获取详情页内容发生了错误:', err);
complete&&complete();
} else {
console.log('获取详情页内容结束');
complete&&complete();
}
});
};
//getDetailURL();
var SQL='INSERT INTO `bookdetail` (title,publicInfo,description,format,thumb,catalog,score,source) VALUES (?,?,?,?,?,?,?,?)';
function storeData(DATA,storeCallback){
//存储数据
connection.query(SQL,DATA, (err, rows, fields) => {
if (err){
console.log('存储出错');
callback();
//throw err;
} else{
console.log('存储一条数据完成');
storeCallback&&storeCallback();
}
});
}
async.waterfall([getDetailURL, getDetailContents], function() {
console.log('爬取结束!');
connection.end();
});