-
Notifications
You must be signed in to change notification settings - Fork 0
/
b.js
89 lines (81 loc) · 2.42 KB
/
b.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
// 51办卡
const Nightmare = require('nightmare');
const fs = require('fs');
const nightmare = Nightmare({
show: true,
loadTimout: 5000,
});
console.log('----------开始-----------');
const next = (url, page, nx) => {
nightmare
.goto(`${url}p${page}/`)
.wait('#lists')
.evaluate((p) => {
let dd = [];
[].map.call(document.querySelectorAll('#lists li'), li => {
let ddd = null;
ddd = {
title: li.querySelector('.share').getAttribute('data-title'),
description: li.querySelector('.share').getAttribute('data-description'),
date: li.querySelector('.des span').innerText,
author: li.querySelector('.author a:nth-child(2)').innerText,
article: li.querySelector('.share').getAttribute('data-url'),
};
dd.push(ddd);
});
if (document.querySelector('.next') && p < 3) {
return {
canNext: true,
data: dd,
};
} else {
return {
canNext: false,
data: dd,
};
}
}, page)
.then(next => {
return new Promise((resolve, reject) => {
const d = next.data;
let index = d.length;
async function getArticle() {
while(index) {
index--;
console.log('....', d[index].article);
await nightmare
.goto(`https://credit.u51.com${d[index].article}`)
.wait('#nc')
.evaluate(dd => {
return document.querySelector('#nc').innerHTML;
}, d)
.then(nxt => {
d[index].article = nxt;
console.log('>>>>', index);
});
}
resolve({
canNext: next.canNext,
data: d,
});
}
getArticle();
});
})
.then(next => {
let splitTag = next.canNext ? ',' : ']';
const startTag = page == 1 ? '[' : '';
const result = JSON.stringify(next.data);
const resultL = result.length;
fs.appendFile(`${__dirname}/51card`, `${startTag}${result.slice(1).slice(0, resultL - 2)}${splitTag}`, () => {
console.log('已追加');
if(!next.canNext) {
console.log('----------结束-----------');
nightmare.end();
process.exit();
}
});
next.canNext ? nx(url, page + 1, nx, next.data) : null;
});
}
next('https://credit.u51.com/post/', 1, next);