1
+ const superagent = require ( 'superagent' ) ;
2
+ const request = require ( 'request' ) ;
3
+ const cheerio = require ( 'cheerio' ) ;
4
+ const fs = require ( 'fs' ) ;
5
+
6
+
7
+ /**
8
+ * 请求函数
9
+ */
10
+ function reptile ( url , type ) {
11
+ request ( url , function ( error , res , body ) {
12
+ // console.error('error:', error); // Print the error if one occurred
13
+ // console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
14
+ // console.log('body:', body); // Print the HTML for the Google homepage.
15
+ if ( ! error && res . statusCode == 200 ) {
16
+ const $ = cheerio . load ( body )
17
+ const $cardBlock = $ ( '.panel' )
18
+ let arr = [ ]
19
+
20
+ for ( let i = 0 ; i < $cardBlock . length ; i ++ ) {
21
+ let obj = { }
22
+ obj . classify = type + $ ( '.panel-title.card' ) . eq ( i ) . text ( ) . trim ( )
23
+ obj . sites = [ ]
24
+ const length = $ ( '.panel' ) . eq ( 1 ) . find ( '.card-title' ) . length
25
+ for ( let j = 0 ; j < length ; j ++ ) {
26
+ let navItem = { }
27
+ navItem . title = $ ( '.card-title' ) . eq ( j ) . text ( ) . trim ( )
28
+ navItem . href = $ ( '.card .card-heading' ) . eq ( j ) . attr ( 'title' )
29
+ navItem . desc = $ ( '.card .card-body' ) . eq ( j ) . text ( ) . trim ( )
30
+ navItem . logo = 'http://chuangzaoshi.com/' + $ ( '.card-icon img' ) . eq ( j ) . attr ( 'src' )
31
+ obj . sites . push ( navItem )
32
+ }
33
+ arr . push ( obj )
34
+ }
35
+ console . log ( JSON . stringify ( arr ) )
36
+ }
37
+ } ) ;
38
+ }
39
+
40
+ // reptile('http://chuangzaoshi.com/index','[设计]') //设计
41
+ // reptile('http://chuangzaoshi.com/code','[前端]') //前端
42
+ // reptile('http://chuangzaoshi.com/product','[产品]') //产品
43
+ reptile ( 'http://chuangzaoshi.com/operate' , '[运营]' ) //运营
0 commit comments