对代码进行优化,处理耦合

    1. import superagent from 'superagent'
    2. import cheerio from 'cheerio'
    3. interface Course {
    4. descs: string
    5. imgSrc: string | undefined
    6. }
    7. class Crowller {
    8. private url = 'http://zblh.gitee.io/hyxc'
    9. getJsonInfo(html: string) {
    10. const $ = cheerio.load(html)
    11. const courseItems = $('.hide-model .zz-item')
    12. const courseInfos: Course[] = []
    13. courseItems.map((index, element) => {
    14. const descs = $(element).find('p').text()
    15. const imgSrc = $(element).find('img').attr('src')
    16. courseInfos.push({ descs, imgSrc })
    17. })
    18. return {
    19. time: new Date().getTime(),
    20. data: courseInfos
    21. }
    22. }
    23. async getRawHtml() {
    24. const result = await superagent.get(this.url)
    25. return result.text
    26. }
    27. async initSpiderProcess() {
    28. const html = await this.getRawHtml()
    29. const courseResult = this.getJsonInfo(html)
    30. console.log(courseResult)
    31. }
    32. constructor() {
    33. this.initSpiderProcess()
    34. }
    35. }
    36. const crowller = new Crowller()

    将数据进行JSON的存储,添加generateJsonContent 方法

    1. // ts -> d.ts 翻译文件 -> js
    2. import fs from 'fs'
    3. import path from 'path'
    4. import superagent from 'superagent'
    5. import cheerio from 'cheerio'
    6. interface Course {
    7. descs: string
    8. imgSrc: string | undefined
    9. }
    10. interface CourseResult {
    11. time: number
    12. data: Course[]
    13. }
    14. interface Content {
    15. [propName: number]: Course[]
    16. }
    17. class Crowller {
    18. private url = 'http://zblh.gitee.io/hyxc'
    19. getJsonInfo(html: string) {
    20. const $ = cheerio.load(html)
    21. const courseItems = $('.hide-model .zz-item')
    22. const courseInfos: Course[] = []
    23. courseItems.map((index, element) => {
    24. const descs = $(element).find('p').text()
    25. const imgSrc = $(element).find('img').attr('src')
    26. courseInfos.push({ descs, imgSrc })
    27. })
    28. return {
    29. time: new Date().getTime(),
    30. data: courseInfos
    31. }
    32. }
    33. async getRawHtml() {
    34. const result = await superagent.get(this.url)
    35. return result.text
    36. }
    37. // 生成需要保存的 JSON
    38. generateJsonContent(courseInfo: CourseResult) {
    39. const filePath = path.resolve(__dirname, '../data/course.json')
    40. let fileContent: Content = {}
    41. // 判断文件路径是否存在,如果存在就把里面的内容存给 fileContent
    42. if (fs.existsSync(filePath)) {
    43. fileContent = JSON.parse(fs.readFileSync(filePath, 'utf-8'))
    44. }
    45. fileContent[courseInfo.time] = courseInfo.data
    46. return fileContent
    47. }
    48. async initSpiderProcess() {
    49. const filePath = path.resolve(__dirname, '../data/course.json')
    50. const html = await this.getRawHtml()
    51. const courseInfo = this.getJsonInfo(html)
    52. const fileContent = this.generateJsonContent(courseInfo)
    53. fs.writeFileSync(filePath, JSON.stringify(fileContent))
    54. }
    55. constructor() {
    56. this.initSpiderProcess()
    57. }
    58. }
    59. const crowller = new Crowller()