11const http = require ( 'http' ) ;
22const fs = require ( 'fs' ) ;
3+
34let request = require ( 'request' ) ;
45let cheerio = require ( 'cheerio' ) ;
6+ let schedule = require ( 'node-schedule' ) ;
7+
8+ let ipModel = require ( './db/models_ip' ) ;
59
610let ipListURL = 'http://www.xicidaili.com/nn/' ;
711let pageSize = 0 ;
@@ -18,6 +22,11 @@ function startRequest(ipListURL) {
1822 } ) ;
1923
2024 res . on ( 'end' , function ( ) {
25+ ipModel . remove ( { } , function ( err ) {
26+ if ( err ) return console . error ( err ) ;
27+ console . log ( '清空数据' ) ;
28+ } ) ;
29+
2130 pageSize ++ ;
2231 $ = cheerio . load ( html ) ;
2332
@@ -31,11 +40,34 @@ function startRequest(ipListURL) {
3140 let testip = request . defaults ( { 'proxy' : proxy } ) . get ( 'http://ip.chinaz.com/getip.aspx' , { timeout : 3000 } , function ( err , response , body ) {
3241 if ( body && body . substring ( 0 , 4 ) == '{ip:' ) {
3342 proxy = proxy + '\n' ;
34- fs . appendFile ( './data/' + 'ipList' + '.txt' , proxy , 'utf-8' , function ( err ) {
35- if ( err ) {
36- console . log ( err ) ;
37- }
43+
44+ let ipOne = new ipModel ( {
45+ country : td . eq ( 0 ) . find ( 'img' ) . attr ( 'alt' ) ,
46+ ip : td . eq ( 1 ) . text ( ) ,
47+ port : td . eq ( 2 ) . text ( ) ,
48+ area : td . eq ( 3 ) . find ( 'a' ) . text ( ) ,
49+ types : td . eq ( 4 ) . text ( ) ,
50+ protocol : td . eq ( 5 ) . text ( ) ,
51+ speed : td . eq ( 6 ) . find ( '.bar' ) . attr ( 'title' ) ,
52+ time : td . eq ( 7 ) . find ( '.bar' ) . attr ( 'title' ) ,
3853 } ) ;
54+
55+ ipOne . save ( function ( err ) {
56+ if ( err ) return console . error ( err ) ;
57+ console . log ( '写入成功!' ) ;
58+
59+ ipModel . find ( function ( err , docs ) {
60+ if ( err ) return console . error ( err ) ;
61+ console . log ( docs ) ;
62+ } ) ;
63+ } ) ;
64+
65+
66+ // fs.appendFile('./data/' + 'ipList' + '.txt', proxy, 'utf-8', function (err) {
67+ // if (err) {
68+ // console.log(err);
69+ // }
70+ // });
3971 }
4072
4173 // console.log(body);
@@ -71,6 +103,9 @@ function startRequest(ipListURL) {
71103 } ) ;
72104 } ) ;
73105}
74- startRequest ( ipListURL ) ;
106+ var everyDay = schedule . scheduleJob ( '0 0 0 * * ?' , function ( ) {
107+ console . log ( 'everyDay 0:00:00' ) ;
108+ startRequest ( ipListURL ) ;
109+ } ) ;
75110
76111module . exports = ipListArray ;
0 commit comments