-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
48 lines (41 loc) · 1.12 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import axios from 'axios'
import cheerio from 'cheerio'
interface Course {
title: string,
count: number
}
class Crawller {
private url = 'https://coding.imooc.com/?c=photo'
constructor() {
this.crawl()
}
async crawl() {
const html = await this.getRawHTML()
const data = this.analyze(html)
console.log(data)
}
async getRawHTML() {
const result = await axios.get(this.url)
return result.data
}
analyze(html: string) {
const data: Course[] = []
const $ = cheerio.load(html)
const $course = $('.shizhan-course-wrap')
$course.map((index, element) => {
const $element = $(element)
const $title = $element.find('.shizan-name')
const $info = $element.find('.shizhan-info span')
const len = $info.length
const $count = $info.eq(len - 2)
const title = $title.text()
const count = parseInt($count.text())
data.push({
title,
count
})
})
return data
}
}
new Crawller()