-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
175 lines (157 loc) · 6.13 KB
/
server.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
const express = require('express');
const cheerio = require('cheerio');
const superagent = require('superagent');
const app = express();
//socket.io
let server = require('http').Server(app);
let io = require('socket.io')(server);
//middleware
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
io.on('connection', function(socket){
console.log('a user connected');
});
server.listen(8080, function () {
console.log('listening on port 8080');
});
//mongoose
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/loupan');
mongoose.Promise = global.Promise;
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log("db connected.")
});
const url = 'https://cd.fang.lianjia.com/loupan/';
let total = 0;
//模型
const loupan = mongoose.model('loupan',{
src: String,
name: String,
where: String,
area: String,
tags: [String],
type: String,
status: String,
price: String,
href: String
});
io.on('connection', (socket) => {
function getPageCount(){
console.log('crawling total page...' + url);
return new Promise(function (resolve, reject) {
superagent.get(url)
.end(function (err, sres) {
if (err) {
console.log(err);
console.log(`error, continue crawling total page...`);
getCount(1, total);
// return reject(err);
}
if(sres){
const $ = cheerio.load(sres.text);
total = Math.floor(JSON.parse($('.page-box').attr('data-total-count')) / 10);
console.log('page:' + total);
resolve(total);
}
});
});
}
function getPageInfo(page){
const pageUrl = page===1?url:`${url}pg${page}`;
console.log('crawling...' + pageUrl);
return new Promise(function (resolve, reject) {
superagent.get(pageUrl)
.end(function (err, sres) {
if (err) {
console.log(`error,continue crawling from (${page})...`);
getInfo(page, total);
// return reject(err);
}
if(sres){
const $ = cheerio.load(sres.text);
const items = [];
$('.resblock-list').each(function (index, element) {
let tagArr = [];
let typeArr = [];
const $element = $(element);
const name = $element.find('.resblock-name a').text();
const imageSrc = $element.find('.resblock-img-wrapper img').attr('data-original');
const location = $element.find('.resblock-location a').text();
const area = $element.find('.resblock-location :nth-child(3)').text();
$element.find('.resblock-tag span').each(function (i, item) {
tagArr[i] = $(item).text().replace(/(\t)|(\n)|(\s+)/g,'');
});
const type = $element.find('.resblock-type').text();
const status = $element.find('.sale-status').text();
const price = $element.find('.main-price :first-child').text();
const href = $element.find('.resblock-name a').attr('href');
console.log(href);
const $eleInfo = {
src: imageSrc,
name: name,
where: location,
area: area,
tags: tagArr,
type: type,
status: status,
price: price,
href: href
};
loupan.create($eleInfo, function (err) {
if(err) console.log(err);
});
items.push($eleInfo);
});
resolve(items);
}
});
})
}
async function getCount() {
socket.emit('progress', { page: `crawling total...` });
await getPageCount();
socket.emit('progress', { page: `total page crawled:${total}!` });
getInfo(1, total);
}
async function getInfo(start, total) {
for(let i = start;i <= total;i++){
socket.emit('progress', { progress: `crawling ${i} page...` });
const pageInfo = await getPageInfo(i);
console.log(pageInfo);
socket.emit('progress', { progress: `finished crawling ${i}page!` });
}
console.log('=================== finished ===================');
socket.emit('progress', { progress: 'finished' });
}
socket.on('request', function (request) {
console.log(request);
loupan.remove({},function (err) {
if(err) console.log(err);
});
getCount();
});
});
app.get('/', function (req, res) {
res.sendFile(__dirname + '/public/index.html');
});
app.get('/api/show/', function (req, res) {
loupan.find({})
.exec((err, result) => {
if (err) console.log(err);
else {
res.send(JSON.stringify(result));
}
});
});
//设置跨域访问
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1');
res.header("Content-Type", "application/json;charset=utf-8");
next();
});