`import * as superagent from ‘superagent’
import * as cheerio from ‘cheerio’
import * as path from ‘path’
import * as fs from ‘fs’
interface Course {
title: string,
count: number
}
interface CourserResult {
time: number,
data: Course[]
}
interface Content {
[propName: number]: Course[]
}
class Crowller {
private secret = ‘secretKey’
private url = ’http://www.dell-lee.com/typescript/demo.html?secret=${this.secret}‘
private getCouseInfo (html: string) {
const $ = cheerio.load(html)
const couseItem = $(’.course-item’)
let courseInfos: Course[] = []
couseItem.map((index, element) => {
const descs = $(element).find(’.course-desc’)
const title: string = descs.eq(0).text()
const count: number = +descs.eq(1).text().split(’:’)[1]
courseInfos = […courseInfos, {
title,
count
}]
})
return {
time: (new Date()).getTime(),
data: courseInfos
}
}
private async getRawHtml() {
const res = await superagent.get(this.url)
return res.text
}
private generateJsonContent (courseInfo: CourserResult) {
const filePath: string = path.resolve(__dirname, ‘…/data/course.json’)
let fileContent: Content = {}
if (fs.existsSync(filePath)) {
fileContent = JSON.parse(fs.readFileSync(filePath, ‘utf-8’))
}
fileContent[courseInfo.time] = courseInfo.data
console.log(fileContent)
fs.writeFileSync(filePath, fileContent, ‘utf-8’)
}
private async initSpiderProcess () {
const html: string = await this.getRawHtml()
const courseInfo: CourserResult = await this.getCouseInfo(html)
this.generateJsonContent(courseInfo)
}
constructor () {
this.initSpiderProcess()
}
}
const crowller = new Crowller()`