双色球大乐透数据获取并保存json

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
const request = require('request')
const { JSDOM } = require('jsdom')
const fs = require('fs')

let dataArr = []
let type = '大乐透';//双色球 大乐透
//获取页面数据
function init(url){
request(url,(err,res,body)=>{
if(res.statusCode == 200){
console.log('请求页面成功!');
const dom = new JSDOM(body)
const document = dom.window.document;
const tbody = document.querySelectorAll('#tdata')[0].querySelectorAll('tr')
console.log('格式转换中...');
tbody.forEach((v,i)=>{
//console.log(v.querySelectorAll('td')[0].innerHTML,i);
dataArr[i] = []
v.querySelectorAll('td').forEach((w,j)=>{
dataArr[i][j] = w.innerHTML
//console.log(w.innerHTML,j);
})
})

// 调用fs.writeFile()方法,写入文件的内容
fs.writeFile(`./${type}.json`,JSON.stringify(dataArr),function(err){
if(!err){
console.log('保存成功!');
}
})
}else{
console.log('页面请求失败!');
}
})
}
let dlt = 'http://datachart.500.com/dlt/history/newinc/history.php?limit=200000&sort=0'
let ssq = 'http://datachart.500.com/ssq/history/newinc/history.php?limit=200000&sort=0'
init(type == '双色球' ? ssq : dlt)