var http = require('http') var fs = require('fs') var cheerio = require('cheerio'); var request = require('request'); var url = "http://www.fortnitechests.info/app/challenges/challenges.html" var resUrl = "http://www.fortnitechests.info/" var Data = [] var $ = null function startRequest(url) { console.log('开始下载...') http.get(url, (res) => { let html = '' res.on('data', (data) => { html += data }) res.on('end', () => { $ = cheerio.load(html) getPageImg(html) getWeekInfo(html) }) }) } function getPageImg(html) { let picArr = [] $('img').each(function() { let imgPath = $(this).attr('src') let imgSrc = resUrl + imgPath let imgName = imgPath.split('/').pop() request(imgSrc).pipe(fs.createWriteStream('./assets/' + imgName)) }) console.log('下载完成...') } function getWeekInfo(html) { $('div[role="tablist"]').each(function() { $(this).find('tbody tr').each(function() { let $td = $(this).find('td') let $img = $td.eq(0).find('img') let icon = [] $img.each(function() { let picName = $(this).attr('src').split('/').pop() icon.push(picName) }) let challenge = $td.eq(1).html() let reward = $td.eq(2).html() console.log(challenge) Data.push({ challenge, reward, icon }) }) }) fs.writeFile('data.json', JSON.stringify(Data), err => { console.log('data.json创建成功') }) } startRequest(url) console.log('server start...')