-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspider.js
49 lines (43 loc) · 1.08 KB
/
spider.js
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
49
var https = require('https')
var url = 'https://www.lagou.com'
var cheerio = require('cheerio')
function filter(html){
var $=cheerio.load(html)
//变成了jQuery
var menu = $('.menu_main')
var menuData = []
menu.each(function(index,value){
var menuTitle = $(value).find('h2').text()
var menuLists = $(value).find('a')
var menuList = []
menuLists.each(function(index,value){
menuList.push($(value).text())
})
menuData.push({
menuTitle:menuTitle,
menuList:menuList
})
})
return menuData
}
function print (menu){
menu.forEach(function(value){
console.log(value.menuTitle)
value.menuList.forEach(function(value){
console.log('-'+value)
})
})
}
https.get(url,function(res){
var html = ''
res.on('data',function(data){
//涉及到trigger个 触法 data 时间
html+=data
})
res.on('end',function(){
print(filter(html))
})
res.on('error',function(err){
console.log(err)
})
})