-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
44 lines (40 loc) · 1.42 KB
/
index.js
File metadata and controls
44 lines (40 loc) · 1.42 KB
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
const table = require('./modules/savaData.js')
const axios = require('axios')
const download = require('./modules/saveImg.js')
let datas = []
let currPage = 1
let goOn = true
const req = axios.create({
baseURL: 'https://www.douban.com/',
timeout: 10000,
headers: {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
}
})
function fetchGameList (success, fail) {
req.get('j/ilmen/game/search?sort=rating&more=' + currPage).then(success).catch(fail)
}
function fetchGameListSuccess (res) {
currPage++
datas = datas.concat(res.data.games)
console.log('------------------获取列表 当前为第' + (currPage - 1) + '页---------------')
console.log('当前数据条数:' + datas.length)
console.log('数据总数:' + res.data.total)
if (goOn) {
fetchGameList(fetchGameListSuccess, fetchGameListfailed)
}
}
function fetchGameListfailed (res) {
if (res.response.status === 500) { // 为500时候没有数据了,开始存入数据库
goOn = false
console.log('--------------------------列表爬取完毕------------------------')
console.log(datas.length)
table.save(datas, 0)
download.downloadImg(datas, 0)
} else {
console.log('----------------------------未知错误--------------------------')
}
}
download.clearDir(function () {
fetchGameList(fetchGameListSuccess, fetchGameListfailed)
})