index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. var http = require('http')
  2. var fs = require('fs')
  3. var cheerio = require('cheerio');
  4. var request = require('request');
  5. var url = "http://www.fortnitechests.info/app/challenges/challenges.html"
  6. var resUrl = "http://www.fortnitechests.info/"
  7. var Data = []
  8. var $ = null
  9. function startRequest(url) {
  10. console.log('开始下载...')
  11. http.get(url, (res) => {
  12. let html = ''
  13. res.on('data', (data) => {
  14. html += data
  15. })
  16. res.on('end', () => {
  17. $ = cheerio.load(html)
  18. getPageImg(html)
  19. getWeekInfo(html)
  20. })
  21. })
  22. }
  23. function getPageImg(html) {
  24. let picArr = []
  25. $('img').each(function() {
  26. let imgPath = $(this).attr('src')
  27. let imgSrc = resUrl + imgPath
  28. let imgName = imgPath.split('/').pop()
  29. request(imgSrc).pipe(fs.createWriteStream('./assets/' + imgName))
  30. })
  31. console.log('下载完成...')
  32. }
  33. function getWeekInfo(html) {
  34. $('div[role="tablist"]').each(function() {
  35. $(this).find('tbody tr').each(function() {
  36. let $td = $(this).find('td')
  37. let $img = $td.eq(0).find('img')
  38. let icon = []
  39. $img.each(function() {
  40. let picName = $(this).attr('src').split('/').pop()
  41. icon.push(picName)
  42. })
  43. let challenge = $td.eq(1).html()
  44. let reward = $td.eq(2).html()
  45. console.log(challenge)
  46. Data.push({
  47. challenge,
  48. reward,
  49. icon
  50. })
  51. })
  52. })
  53. fs.writeFile('data.json', JSON.stringify(Data), err => {
  54. console.log('data.json创建成功')
  55. })
  56. }
  57. startRequest(url)
  58. console.log('server start...')