Last active
November 13, 2020 09:06
-
-
Save luojiyin1987/a8552e41c9ca61bb9452dc08c9d1795f to your computer and use it in GitHub Desktop.
crowller superagent cheerio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import superagent from 'superagent'; | |
| import cheerio from 'cheerio'; | |
| interface Course{ | |
| title: string; | |
| count: number; | |
| } | |
| class Crowller{ | |
| private secret='demo'; | |
| private url = `http://www.dell-lee.com/typescript/demo.html?secret=${this.secret}`; | |
| private rawHtml=''; | |
| async getRawHtml(){ | |
| const result = await superagent.get(this.url) | |
| //console.log("re", result.text); | |
| this.getCourseInfo(result.text); | |
| } | |
| getCourseInfo(html:string){ | |
| const coureInfos: Course[]=[]; | |
| const $ =cheerio.load(html); | |
| const courseItems = $('.course-item') | |
| console.log(courseItems.length); | |
| courseItems.map((index, element)=> { | |
| const descs = $(element).find('.course-desc'); | |
| const title = descs.eq(0).text(); | |
| const count = parseInt( | |
| descs | |
| .eq(1) | |
| .text() | |
| .split(':')[1] | |
| ) | |
| coureInfos.push({title, count}) | |
| }) | |
| console.log('coureInfo', coureInfos); | |
| } | |
| constructor(){ | |
| console.log('constuctor'); | |
| this.getRawHtml(); | |
| } | |
| } | |
| const crowller = new Crowller(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment