用TypeScript开发爬虫程序

简介:


全局安装typescript:


 
 
  1. npm install -g typescript 

目前版本2.0.3,这个版本不再需要使用typings命令了。但是vscode捆绑的版本是1.8的,需要一些配置工作,看本文的处理办法。

测试tsc命令:


 
 
  1. tsc 

创建要写的程序项目文件夹:


 
 
  1. mkdir test-typescript-spider 

进入该文件夹:


 
 
  1. cd test-typescript-spider 

初始化项目:


 
 
  1. npm init 

安装superagent和cheerio模块:


 
 
  1. npm i --save superagent cheerio 

安装对应的类型声明模块:


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

安装项目内的typescript(必须走这一步):


 
 
  1. npm i --save typescript 

用vscode打开项目文件夹。在该文件夹下创建tsconfig.json文件,并复制以下配置代码进去:


 
 
  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打开“文件”-“首选项”-“工作区设置”在settings.json中加入(如果不做这个配置,vscode会在打开项目的时候提示选择哪个版本的typescript):


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

创建api.ts文件,复制以下代码进去:


 
 
  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. }  

创建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();  

执行命令:


 
 
  1. tsc 

然后:


 
 
  1. node out/app 

观察输出是否正确。

现在尝试抓取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();  

观察输出,文章的标题和链接都已获取到了。

现在尝试深入抓取文章内容


 
 
  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();  

可以发现因为访问服务器太迅猛,导致出现很多次503错误。

解决:

添加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(); 

观察输出可以看到,程序实现了隔一秒再请求下一个内容页。

现在尝试把抓取到的东西存到数据库中。安装mongoose模块:


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

然后建立Scheme。先创建models文件夹:


 
 
  1. mkdir models 

在models文件夹下创建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文件夹下创建IArticle.ts:


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

在models文件夹下创建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. })();  

执行


 
 
  1. tsc 
  2. node out/app  

观察输出,并去数据库检查一下可以发现入库成功了!

补充:remote_get方法的改进版,实现错误重试和加入代理服务器.放弃了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.     //每次请求都先稍等一下 
  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合并为一个文件,可能更好,可以参考我另一个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);  

导入的时候这么写就行了:


 
 
  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用于实体对象的传递,例如


 
 
  1. function xxx(p:IProxy){ 
  2. }  



作者:sagacite

来源:51CTO

相关文章
|
2月前
|
数据采集 存储 缓存
PHP爬虫的使用与开发
本文深入探讨了PHP爬虫的使用与开发,涵盖基本原理、关键技术、开发实践及优化策略。从发送HTTP请求、解析HTML到数据存储,再到处理反爬机制,全面指导读者构建高效可靠的爬虫程序。
87 3
|
6天前
|
JavaScript 数据安全/隐私保护
Vue Amazing UI 组件库(Vue3+TypeScript+Vite 等最新技术栈开发)
Vue Amazing UI 是一个基于 Vue 3、TypeScript、Vite 等最新技术栈开发构建的现代化组件库,包含丰富的 UI 组件和常用工具函数,并且持续不断维护更新中。另外,组件库全量使用 TypeScript,支持自动按需引入和 Tree Shaking 等,能够显著提升开发效率,降低开发成本。
29 5
Vue Amazing UI 组件库(Vue3+TypeScript+Vite 等最新技术栈开发)
|
4月前
|
JavaScript 前端开发 安全
TypeScript的优势与实践:提升JavaScript开发效率
【10月更文挑战第8天】TypeScript的优势与实践:提升JavaScript开发效率
|
4月前
|
JavaScript 前端开发 IDE
深入理解TypeScript:提升JavaScript开发的利器
【10月更文挑战第8天】 深入理解TypeScript:提升JavaScript开发的利器
51 0
|
1月前
|
存储 人工智能 开发框架
Eliza:TypeScript 版开源 AI Agent 开发框架,快速搭建智能、个性的 Agents 系统
Eliza 是一个开源的多代理模拟框架,支持多平台连接、多模型集成,能够快速构建智能、高效的AI系统。
224 8
Eliza:TypeScript 版开源 AI Agent 开发框架,快速搭建智能、个性的 Agents 系统
|
1月前
|
数据采集 存储 JavaScript
jsdom爬虫程序中eBay主页内容爬取的异步处理
jsdom爬虫程序中eBay主页内容爬取的异步处理
|
2月前
|
数据采集 JavaScript 前端开发
异步请求在TypeScript网络爬虫中的应用
异步请求在TypeScript网络爬虫中的应用
|
3月前
|
传感器 JavaScript 前端开发
利用TypeScript提升代码质量和开发效率
TypeScript作为JavaScript的超集,通过引入静态类型系统和面向对象特性,显著提升了代码质量和开发效率。本文介绍了TypeScript的基本概念、优势及最佳实践,包括基础类型注解、接口与类的使用、类型推断、高级类型、装饰器应用及现代工具的集成,帮助开发者构建更健壮的应用程序。
|
3月前
|
开发框架 JavaScript 前端开发
TypeScript 是一种静态类型的编程语言,它扩展了 JavaScript,为 Web 开发带来了强大的类型系统、组件化开发支持、与主流框架的无缝集成、大型项目管理能力和提升开发体验等多方面优势
TypeScript 是一种静态类型的编程语言,它扩展了 JavaScript,为 Web 开发带来了强大的类型系统、组件化开发支持、与主流框架的无缝集成、大型项目管理能力和提升开发体验等多方面优势。通过明确的类型定义,TypeScript 能够在编码阶段发现潜在错误,提高代码质量;支持组件的清晰定义与复用,增强代码的可维护性;与 React、Vue 等框架结合,提供更佳的开发体验;适用于大型项目,优化代码结构和性能。随着 Web 技术的发展,TypeScript 的应用前景广阔,将继续引领 Web 开发的新趋势。
70 2
|
3月前
|
JavaScript 前端开发 安全
掌握TypeScript:提升JavaScript开发质量
本文介绍了TypeScript如何通过其静态类型系统、面向对象特性及对现代JavaScript特性的支持,提升JavaScript开发的质量,包括减少错误、增强代码可维护性和利用类型推断等功能,适用于大型项目开发。

热门文章

最新文章