- 通过 cheerio npm包可以用类似jq的语法来进行数据提取
- 创建 getJsonInfo 方法,来进行数据的处理 ```typescript import superagent from ‘superagent’ import cheerio from ‘cheerio’
interface Course { descs: string imgSrc: string | undefined }
class Crowller { private url = ‘http://zblh.gitee.io/hyxc‘
getJsonInfo(html: string) { const $ = cheerio.load(html) const courseItems = $(‘.hide-model .zz-item’) const courseInfos: Course[] = [] courseItems.map((index, element) => { const descs = $(element).find(‘p’).text() const imgSrc = $(element).find(‘img’).attr(‘src’) courseInfos.push({ descs, imgSrc }) }) const result = { time: new Date().getTime(), data: courseInfos } console.log(result) }
async getRawHtml() { const result = await superagent.get(this.url) this.getJsonInfo(result.text) } constructor() { this.getRawHtml() } }
const crowller = new Crowller()
```
