自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用TypeScript開發(fā)爬蟲程序

開發(fā) 前端
目前版本2.0.3,這個(gè)版本不再需要使用typings命令了。但是vscode捆綁的版本是1.8的,需要一些配置工作,看本文的處理辦法。

[[175063]]

全局安裝typescript:

  1. npm install -g typescript 

目前版本2.0.3,這個(gè)版本不再需要使用typings命令了。但是vscode捆綁的版本是1.8的,需要一些配置工作,看本文的處理辦法。

測試tsc命令:

  1. tsc 

創(chuàng)建要寫的程序項(xiàng)目文件夾:

  1. mkdir test-typescript-spider 

進(jìn)入該文件夾:

  1. cd test-typescript-spider 

初始化項(xiàng)目:

  1. npm init 

安裝superagent和cheerio模塊:

  1. npm i --save superagent cheerio 

安裝對(duì)應(yīng)的類型聲明模塊:

  1. npm i -s @types/superagent --save  
  2. npm i -s @types/cheerio --save  

安裝項(xiàng)目內(nèi)的typescript(必須走這一步):

  1. npm i --save typescript 

用vscode打開項(xiàng)目文件夾。在該文件夾下創(chuàng)建tsconfig.json文件,并復(fù)制以下配置代碼進(jìn)去:

  1.     "compilerOptions": { 
  2.         "target""ES6"
  3.         "module""commonjs"
  4.         "noEmitOnError"true
  5.         "noImplicitAny"true
  6.         "experimentalDecorators"true
  7.         "sourceMap"false
  8.      // "sourceRoot""./"
  9.         "outDir""./out" 
  10.     }, 
  11.     "exclude": [ 
  12.         "node_modules" 
  13.     ] 
  14.  

在vscode打開“文件”-“***項(xiàng)”-“工作區(qū)設(shè)置”在settings.json中加入(如果不做這個(gè)配置,vscode會(huì)在打開項(xiàng)目的時(shí)候提示選擇哪個(gè)版本的typescript):

  1.    "typescript.tsdk""node_modules/typescript/lib" 
  2.  

創(chuàng)建api.ts文件,復(fù)制以下代碼進(jìn)去:

  1. import superagent = require('superagent'); 
  2. import cheerio = require('cheerio'); 
  3.  
  4. export const remote_get = function(url: string) { 
  5.  
  6.     const promise = new Promise<superagent.Response>(function (resolve, reject) { 
  7.         superagent.get(url) 
  8.             .end(function (err, res) { 
  9.                 if (!err) { 
  10.                     resolve(res); 
  11.                 } else { 
  12.                     console.log(err) 
  13.                     reject(err); 
  14.                 } 
  15.             }); 
  16.     }); 
  17.     return promise; 
  18.  

創(chuàng)建app.ts文件,書寫測試代碼:

  1. import api = require('./api'); 
  2. const go = async () => { 
  3.     let res = await api.remote_get('http://www.baidu.com/'); 
  4.     console.log(res.text); 
  5. go();  

執(zhí)行命令:

  1. tsc 

然后:

  1. node out/app 

觀察輸出是否正確。

現(xiàn)在嘗試抓取http://cnodejs.org/的***頁文章鏈接。

修改app.ts文件,代碼如下:

  1. import api = require('./api'); 
  2. import cheerio = require('cheerio'); 
  3.  
  4. const go = async () => { 
  5.     const res = await api.remote_get('http://cnodejs.org/'); 
  6.     const $ = cheerio.load(res.text); 
  7.     let urls: string[] = []; 
  8.     let titles: string[] = []; 
  9.     $('.topic_title_wrapper').each((index, element) => { 
  10.         titles.push($(element).find('.topic_title').first().text().trim()); 
  11.         urls.push('http://cnodejs.org/' + $(element).find('.topic_title').first().attr('href')); 
  12.     }) 
  13.     console.log(titles, urls); 
  14. go();  

觀察輸出,文章的標(biāo)題和鏈接都已獲取到了。

現(xiàn)在嘗試深入抓取文章內(nèi)容

  1. import api = require('./api'); 
  2. import cheerio = require('cheerio'); 
  3.  
  4. const go = async () => { 
  5.     const res = await api.remote_get('http://cnodejs.org/'); 
  6.     const $ = cheerio.load(res.text); 
  7.     $('.topic_title_wrapper').each(async (index, element) => { 
  8.         let url = ('http://cnodejs.org' + $(element).find('.topic_title').first().attr('href')); 
  9.         const res_content = await api.remote_get(url); 
  10.         const $_content = cheerio.load(res_content.text); 
  11.         console.log($_content('.topic_content').first().text()); 
  12.     }) 
  13.  
  14. go();  

可以發(fā)現(xiàn)因?yàn)樵L問服務(wù)器太迅猛,導(dǎo)致出現(xiàn)很多次503錯(cuò)誤。

解決:

添加helper.ts文件:

  1. export const wait_seconds = function (senconds: number) { 
  2.     return new Promise(resolve => setTimeout(resolve, senconds * 1000)); 
  3.  

修改api.ts文件為:

  1. import superagent = require('superagent'); 
  2. import cheerio = require('cheerio'); 
  3.  
  4. export const get_index_urls = function () { 
  5.     const res = await remote_get('http://cnodejs.org/'); 
  6.     const $ = cheerio.load(res.text); 
  7.     let urls: string[] = []; 
  8.     $('.topic_title_wrapper').each(async (index, element) => { 
  9.         urls.push('http://cnodejs.org' + $(element).find('.topic_title').first().attr('href')); 
  10.     }); 
  11.     return urls; 
  12. export const get_content = async function (url: string) { 
  13.     const res = await remote_get(url); 
  14.     const $ = cheerio.load(res.text); 
  15.     return $('.topic_content').first().text(); 
  16.  
  17. export const remote_get = function (url: string) { 
  18.  
  19.     const promise = new Promise<superagent.Response>(function (resolve, reject) { 
  20.  
  21.         superagent.get(url) 
  22.             .end(function (err, res) { 
  23.                 if (!err) { 
  24.                     resolve(res); 
  25.                 } else { 
  26.                     console.log(err) 
  27.                     reject(err); 
  28.                 } 
  29.             }); 
  30.     }); 
  31.     return promise; 

修改app.ts文件為:

  1. import api = require('./api'); 
  2. import helper = require('./helper'); 
  3. import cheerio = require('cheerio'); 
  4.  
  5. const go = async () => { 
  6.     let urls = await api.get_index_urls(); 
  7.     for (let i = 0; i < urls.length; i++) { 
  8.         await helper.wait_seconds(1); 
  9.         let text = await api.get_content(urls[i]); 
  10.         console.log(text); 
  11.     } 
  12. go(); 

觀察輸出可以看到,程序?qū)崿F(xiàn)了隔一秒再請(qǐng)求下一個(gè)內(nèi)容頁。

現(xiàn)在嘗試把抓取到的東西存到數(shù)據(jù)庫中。安裝mongoose模塊:

  1. npm i mongoose --save 
  2. npm i -s @types/mongoose --save  

然后建立Scheme。先創(chuàng)建models文件夾:

  1. mkdir models 

在models文件夾下創(chuàng)建index.ts:

  1. import * as mongoose from 'mongoose'
  2.  
  3. mongoose.connect('mongodb://127.0.0.1/cnodejs_data', { 
  4.     server: { poolSize: 20 } 
  5. }, function (err) { 
  6.     if (err) { 
  7.         process.exit(1); 
  8.     } 
  9. }); 
  10.  
  11. // models 
  12. export const Article = require('./article');  

在models文件夾下創(chuàng)建IArticle.ts:

  1. interface IArticle { 
  2.     title: String; 
  3.     url: String; 
  4.     text: String; 
  5. export = IArticle; 

在models文件夾下創(chuàng)建Article.ts:

  1. import mongoose = require('mongoose'); 
  2. import IArticle = require('./IArticle'); 
  3. interface IArticleModel extends IArticle, mongoose.Document { } 
  4.  
  5. const ArticleSchema = new mongoose.Schema({ 
  6.     title: { type: String }, 
  7.     url: { type: String }, 
  8.     text: { type: String }, 
  9. }); 
  10.  
  11. const Article = mongoose.model<IArticleModel>("Article", ArticleSchema); 
  12. export = Article;  

修改api.ts為:

  1. import superagent = require('superagent'); 
  2. import cheerio = require('cheerio'); 
  3. import models = require('./models'); 
  4. const Article = models.Article; 
  5.  
  6. export const get_index_urls = async function () { 
  7.     const res = await remote_get('http://cnodejs.org/'); 
  8.  
  9.     const $ = cheerio.load(res.text); 
  10.     let urls: string[] = []; 
  11.     $('.topic_title_wrapper').each((index, element) => { 
  12.         urls.push('http://cnodejs.org' + $(element).find('.topic_title').first().attr('href')); 
  13.     }); 
  14.     return urls; 
  15.  
  16. export const fetch_content = async function (url: string) { 
  17.     const res = await remote_get(url); 
  18.  
  19.     const $ = cheerio.load(res.text); 
  20.     let article = new Article(); 
  21.     article.text = $('.topic_content').first().text(); 
  22.     article.title = $('.topic_full_title').first().text().replace('置頂''').replace('精華''').trim(); 
  23.     article.url = url; 
  24.     console.log('獲取成功:' + article.title); 
  25.     article.save(); 
  26.  
  27. export const remote_get = function (url: string) { 
  28.  
  29.     return new Promise<superagent.Response>((resolve, reject) => { 
  30.         superagent.get(url) 
  31.             .end(function (err, res) { 
  32.                 if (!err) { 
  33.                     resolve(res); 
  34.                 } else { 
  35.                     reject(err); 
  36.                 } 
  37.             }); 
  38.     }); 
  39.  

修改app.ts為:

  1. import api = require('./api'); 
  2. import helper = require('./helper'); 
  3. import cheerio = require('cheerio'); 
  4.  
  5. (async () => { 
  6.  
  7.     try { 
  8.         let urls = await api.get_index_urls(); 
  9.         for (let i = 0; i < urls.length; i++) { 
  10.             await helper.wait_seconds(1); 
  11.             await api.fetch_content(urls[i]); 
  12.         } 
  13.     } catch (err) { 
  14.         console.log(err); 
  15.     } 
  16.  
  17.     console.log('完畢!'); 
  18.  
  19. })();  

執(zhí)行

  1. tsc 
  2. node out/app  

觀察輸出,并去數(shù)據(jù)庫檢查一下可以發(fā)現(xiàn)入庫成功了!

補(bǔ)充:remote_get方法的改進(jìn)版,實(shí)現(xiàn)錯(cuò)誤重試和加入代理服務(wù)器.放棄了superagent庫,用的request庫,僅供參考:

  1. //config.retries = 3; 
  2. let current_retry = config.retries || 0; 
  3. export const remote_get = async function (url: string, proxy?: string) { 
  4.     //每次請(qǐng)求都先稍等一下 
  5.     await wait_seconds(2); 
  6.     if (!proxy) { 
  7.         proxy = ''
  8.     } 
  9.     const promise = new Promise<string>(function (resolve, reject) { 
  10.         console.log('get: ' + url + ',  using proxy: ' + proxy); 
  11.         let options: request.CoreOptions = { 
  12.             headers: { 
  13.                 'Cookie'''
  14.                 'User-Agent''Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'
  15.                 'Referer''https://www.baidu.com/' 
  16.             }, 
  17.             encoding: 'utf-8'
  18.             method: 'GET'
  19.             proxy: proxy, 
  20.             timeout: 3000, 
  21.         } 
  22.         request(url, options, async function (err, response, body) { 
  23.             console.log('got:' + url); 
  24.             if (!err) { 
  25.                 body = body.toString(); 
  26.                 current_retry = config.retries || 0; 
  27.                 console.log('bytes:' + body.length); 
  28.                 resolve(body); 
  29.             } else { 
  30.                 console.log(err); 
  31.                 if (current_retry <= 0) { 
  32.                     current_retry = config.retries || 0; 
  33.                     reject(err); 
  34.                 } else { 
  35.                     console.log('retry...(' + current_retry + ')'
  36.                     current_retry--; 
  37.                     try { 
  38.                         let body = await remote_get(url, proxy); 
  39.                         resolve(body); 
  40.                     } catch (e) { 
  41.                         reject(e); 
  42.                     } 
  43.                 } 
  44.             } 
  45.         }); 
  46.     }); 
  47.     return promise; 
  48.  

另外,IArticle.ts和Article.ts合并為一個(gè)文件,可能更好,可以參考我另一個(gè)model的寫法:

  1. import mongoose = require('mongoose'); 
  2.  
  3. interface IProxyModel { 
  4.     uri: string; 
  5.     ip: string; 
  6.     port:string; 
  7.     info:string; 
  8. export interface IProxy extends IProxyModel, mongoose.Document { } 
  9.  
  10. const ProxySchema = new mongoose.Schema({ 
  11.     uri: { type: String },// 
  12.     ip: { type: String },// 
  13.     port: { type: String },// 
  14.     info: { type: String },// 
  15. }); 
  16. export const Proxy = mongoose.model<IProxy>("Proxy", ProxySchema);  

導(dǎo)入的時(shí)候這么寫就行了:

  1. import { IProxy, Proxy } from './models'

其中Proxy可以用來做new、find、where之類的操作:

  1. let x = new Proxy(); 
  2. let xx = await Proxy.find({}); 
  3. let xxx = await Proxy.where('aaa',123).exec();  

而IProxy用于實(shí)體對(duì)象的傳遞,例如

  1. function xxx(p:IProxy){ 
  2.  
責(zé)任編輯:龐桂玉 來源: segmentfault
相關(guān)推薦

2021-05-08 19:16:38

Python開發(fā)Emoji

2023-07-12 08:29:58

TypeScrip元組元素

2010-02-06 14:19:26

ibmdwGoogleMap

2011-08-31 15:15:20

PhoneGap

2010-11-18 15:52:32

QMLMeeGo

2011-05-03 09:14:45

QtOvi商店Symbian

2011-01-27 17:33:10

DalvikAndroidQNX

2020-10-14 15:05:02

React應(yīng)用程序

2020-10-09 07:54:43

PythonJava爬蟲

2024-11-27 06:31:02

2020-06-01 09:40:06

開發(fā)ReactTypeScript

2020-06-03 16:50:24

TypeScriptReact前端

2022-09-14 15:24:57

typescript快排

2021-10-11 14:28:25

TypeScript企業(yè)級(jí)應(yīng)用

2009-09-22 12:22:54

ibmdwLotus

2024-05-31 12:31:54

C#爬蟲Python

2021-10-31 21:01:00

數(shù)列TypeScriptJava

2011-03-30 10:44:39

COBOL

2012-03-15 15:35:51

iUI框架EclipseiOS Web

2022-11-07 16:06:15

TypeScript開發(fā)技巧
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)