Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Last active November 13, 2020 09:06
Show Gist options
  • Save luojiyin1987/a8552e41c9ca61bb9452dc08c9d1795f to your computer and use it in GitHub Desktop.
Save luojiyin1987/a8552e41c9ca61bb9452dc08c9d1795f to your computer and use it in GitHub Desktop.
crowller superagent cheerio
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