基于TypeScript从0到1搭建一款爬虫工具

前言

今天,我们将使用TS这门语言搭建一款爬虫工具。目标网址是什么呢?我们去上网一搜,经过几番排查之后,我们选定了这一个网站。

一个视频网站,我们的目的主要是爬取这个网站上视频的播放链接。下面,我们就开始进行第一步。

第一步

俗话说,万事开头难。不过对于这个项目而言,恰恰相反。你需要做以下几个事情:

1.我们需要创建一个项目文件夹

2.键入命令,初始化项目

  1. npm init -y

3.局部安装typescript

  1. npm install typescript -D

4.接着键入命令,生成ts配置文件

  1. tsc –init

5.局部安装ts-node,用于命令行输出命令

  1. npm install -D ts-node

6.在项目文件夹中创建一个src文件夹

然后我们在src文件夹中创建一个crawler.ts文件。

7.在package.json文件中修改快捷启动命令

  1. “scripts”: {
  2.     “dev-t”: “ts-node ./src/crawler.ts”
  3.   }

第二步

接下来,我们将进行实战操作,也就是上文中crawler.ts文件是我们的主战场。

我们首先需要引用的这几个依赖,分别是

  1. import superagent from “superagent”;
  2. import cheerio from “cheerio”;
  3. import fs from “fs”;
  4. import path from “path”;

所以,我们会这样安装依赖:

superagent作用是获取远程网址html的内容。

  1. npm install superagent

cheerio作用是可以通过jQ语法获取页面节点的内容。

  1. npm install cheerio

剩余两个依赖fs,path。它们是node内置依赖,直接引入即可。

我们完成了安装依赖,但是会发现你安装的依赖上会有红色报错。原因是这样的,superagent和cheerio内部都是用JS写的,并不是TS写的,而我们现在的环境是TS。所以我们需要翻译一下,我们将这种翻译文件又称类型定义文件(以.d.ts为后缀)。我们可以使用以下命令安装类型定义文件。

  1. npm install -D @types/superagent
  2. npm install -D @types/cheerio

接下来,我们就认认真真看源码了。

1.安装完两个依赖后,我们需要创建一个Crawler类,并且将其实例化。

  1. import superagent from “superagent”;
  2. import cheerio from “cheerio”;
  3. import fs from “fs”;
  4. import path from “path”;
  5. class Crawler {
  6.   constructor() {
  7.   }
  8. }
  9. const crawler = new Crawler();

2.我们确定下要爬取的网址,然后赋给一个私有变量。最后我们会封装一个getRawHtml方法来获取对应网址的内容。

getRawHtml方法中我们使用了async/await关键字,主要用于异步获取页面内容,然后返回值。

  1. import superagent from “superagent”;
  2. import cheerio from “cheerio”;
  3. import fs from “fs”;
  4. import path from “path”;
  5. class Crawler {
  6.   private url = “https://www.hanju.run/play/39221-4-0.html”;
  7.   async getRawHtml() {
  8.     const result = await superagent.get(this.url);
  9.     return result.text;
  10.   }
  11.   async initSpiderProcess() {
  12.     const html = await this.getRawHtml();
  13.   }
  14.   constructor() {
  15.     this.initSpiderProcess();
  16.   }
  17. }
  18. const crawler = new Crawler();

3.使用cheerio依赖内置的方法获取对应的节点内容。

我们通过getRawHtml方法异步获取网页的内容,然后我们传给getJsonInfo这个方法,注意是string类型。我们这里通过cheerio.load(html)这条语句处理,就可以通过jQ语法来获取对应的节点内容。我们获取到了网页中视频的标题以及链接,通过键值对的方式添加到一个对象中。注:我们在这里定义了一个接口,定义键值对的类型。

  1. import superagent from “superagent”;
  2. import cheerio from “cheerio”;
  3. import fs from “fs”;
  4. import path from “path”;
  5. interface Info {
  6.   name: string;
  7.   url: string;
  8. }
  9. class Crawler {
  10.   private url = “https://www.hanju.run/play/39221-4-0.html”;
  11.   getJsonInfo(html: string) {
  12.     const $ = cheerio.load(html);
  13.     const info: Info[] = [];
  14.     const scpt: string = String($(“.play>script:nth-child(1)”).html());
  15.     const url = unescape(
  16.       scpt.split(“;”)[3].split(“(“)[1].split(“)”)[0].replace(/”/g, “”)
  17.     );
  18.     const name: string = String($(“title”).html());
  19.     info.push({
  20.       name,
  21.       url,
  22.     });
  23.     const result = {
  24.       time: new Date().getTime(),
  25.       data: info,
  26.     };
  27.     return result;
  28.   }
  29.   async getRawHtml() {
  30.     const result = await superagent.get(this.url);
  31.     return result.text;
  32.   }
  33.   async initSpiderProcess() {
  34.     const html = await this.getRawHtml();
  35.     const info = this.getJsonInfo(html);
  36.   }
  37.   constructor() {
  38.     this.initSpiderProcess();
  39.   }
  40. }
  41. const crawler = new Crawler();

4.我们首先要在项目根目录下创建一个data文件夹。然后我们将获取的内容我们存入文件夹内的url.json文件(文件自动生成)中。

我们将其封装成getJsonContent方法,在这里我们使用了path.resolve来获取文件的路径。fs.readFileSync来读取文件内容,fs.writeFileSync来将内容写入文件。注:我们分别定义了两个接口objJson与InfoResult。

  1. import superagent from “superagent”;
  2. import cheerio from “cheerio”;
  3. import fs from “fs”;
  4. import path from “path”;
  5. interface objJson {
  6.   [propName: number]: Info[];
  7. }
  8. interface Info {
  9.   name: string;
  10.   url: string;
  11. }
  12. interface InfoResult {
  13.   time: number;
  14.   data: Info[];
  15. }
  16. class Crawler {
  17.   private url = “https://www.hanju.run/play/39221-4-0.html”;
  18.   getJsonInfo(html: string) {
  19.     const $ = cheerio.load(html);
  20.     const info: Info[] = [];
  21.     const scpt: string = String($(“.play>script:nth-child(1)”).html());
  22.     const url = unescape(
  23.       scpt.split(“;”)[3].split(“(“)[1].split(“)”)[0].replace(/”/g, “”)
  24.     );
  25.     const name: string = String($(“title”).html());
  26.     info.push({
  27.       name,
  28.       url,
  29.     });
  30.     const result = {
  31.       time: new Date().getTime(),
  32.       data: info,
  33.     };
  34.     return result;
  35.   }
  36.   async getRawHtml() {
  37.     const result = await superagent.get(this.url);
  38.     return result.text;
  39.   }
  40.   getJsonContent(info: InfoResult) {
  41.     const filePath = path.resolve(__dirname, “../data/url.json”);
  42.     let fileContent: objJson = {};
  43.     if (fs.existsSync(filePath)) {
  44.       fileContent = JSON.parse(fs.readFileSync(filePath, “utf-8”));
  45.     }
  46.     fileContent[info.time] = info.data;
  47.     fs.writeFileSync(filePath, JSON.stringify(fileContent));
  48.   }
  49.   async initSpiderProcess() {
  50.     const html = await this.getRawHtml();
  51.     const info = this.getJsonInfo(html);
  52.     this.getJsonContent(info);
  53.   }
  54.   constructor() {
  55.     this.initSpiderProcess();
  56.   }
  57. }
  58. const crawler = new Crawler();

5.运行命令

  1. npm run dev-t

6.查看生成文件的效果

  1. {
  2.   “1610738046569”: [
  3.     {
  4.       “name”: “《复仇者联盟4:终局之战》HD1080P中字m3u8在线观看-韩剧网”,
  5.       “url”: “https://wuxian.xueyou-kuyun.com/20190728/16820_302c7858/index.m3u8”
  6.     }
  7.   ],
  8.   “1610738872042”: [
  9.     {
  10.       “name”: “《钢铁侠2》HD高清m3u8在线观看-韩剧网”,
  11.       “url”: “https://www.yxlmbbs.com:65/20190920/54uIR9hI/index.m3u8”
  12.     }
  13.   ],
  14.   “1610739069969”: [
  15.     {
  16.       “name”: “《钢铁侠2》中英特效m3u8在线观看-韩剧网”,
  17.       “url”: “https://tv.youkutv.cc/2019/11/12/mjkHyHycfh0LyS4r/playlist.m3u8”
  18.     }
  19.   ]
  20. }

准结语

到这里真的结束了吗?

不!不!不!

真的没有结束。

我们会看到上面一坨代码,真的很臭~

我们将分别使用组合模式与单例模式将其优化。

优化一:组合模式

组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。

这种模式创建了一个包含自己对象组的类。该类提供了修改相同对象组的方式。

简言之,就是可以像处理简单元素一样来处理复杂元素。

首先,我们在src文件夹下创建一个combination文件夹,然后在其文件夹下分别在创建两个文件crawler.ts和urlAnalyzer.ts。

crawler.ts

crawler.ts文件的作用主要是处理获取页面内容以及存入文件内。

  1. import superagent from “superagent”;
  2. import fs from “fs”;
  3. import path from “path”;
  4. import UrlAnalyzer from “./urlAnalyzer.ts”;
  5. export interface Analyzer {
  6.   analyze: (html: string, filePath: string) => string;
  7. }
  8. class Crowller {
  9.   private filePath = path.resolve(__dirname, “../../data/url.json”);
  10.   async getRawHtml() {
  11.     const result = await superagent.get(this.url);
  12.     return result.text;
  13.   }
  14.   writeFile(content: string) {
  15.     fs.writeFileSync(this.filePath, content);
  16.   }
  17.   async initSpiderProcess() {
  18.     const html = await this.getRawHtml();
  19.     const fileContent = this.analyzer.analyze(html, this.filePath);
  20.     this.writeFile(fileContent);
  21.   }
  22.   constructor(private analyzer: Analyzer, private url: string) {
  23.     this.initSpiderProcess();
  24.   }
  25. }
  26. const url = “https://www.hanju.run/play/39257-1-1.html”;
  27. const analyzer = new UrlAnalyzer();
  28. new Crowller(analyzer, url);

urlAnalyzer.ts

urlAnalyzer.ts文件的作用主要是处理获取页面节点内容的具体逻辑。

  1. import cheerio from “cheerio”;
  2. import fs from “fs”;
  3. import { Analyzer } from “./crawler.ts”;
  4. interface objJson {
  5.   [propName: number]: Info[];
  6. }
  7. interface InfoResult {
  8.   time: number;
  9.   data: Info[];
  10. }
  11. interface Info {
  12.   name: string;
  13.   url: string;
  14. }
  15. export default class UrlAnalyzer implements Analyzer {
  16.   private getJsonInfo(html: string) {
  17.     const $ = cheerio.load(html);
  18.     const info: Info[] = [];
  19.     const scpt: string = String($(“.play>script:nth-child(1)”).html());
  20.     const url = unescape(
  21.       scpt.split(“;”)[3].split(“(“)[1].split(“)”)[0].replace(/”/g, “”)
  22.     );
  23.     const name: string = String($(“title”).html());
  24.     info.push({
  25.       name,
  26.       url,
  27.     });
  28.     const result = {
  29.       time: new Date().getTime(),
  30.       data: info,
  31.     };
  32.     return result;
  33.   }
  34.   private getJsonContent(info: InfoResult, filePath: string) {
  35.     let fileContent: objJson = {};
  36.     if (fs.existsSync(filePath)) {
  37.       fileContent = JSON.parse(fs.readFileSync(filePath, “utf-8”));
  38.     }
  39.     fileContent[info.time] = info.data;
  40.     return fileContent;
  41.   }
  42.   public analyze(html: string, filePath: string) {
  43.     const info = this.getJsonInfo(html);
  44.     console.log(info);
  45.     const fileContent = this.getJsonContent(info, filePath);
  46.     return JSON.stringify(fileContent);
  47.   }
  48. }

可以在package.json文件中定义快捷启动命令。

  1. “scripts”: {
  2.   “dev-c”: “ts-node ./src/combination/crawler.ts”
  3. },

然后使用npm run dev-c启动即可。

优化二:单例模式

**单例模式(Singleton Pattern)**是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。

应用实例:

  • 1、一个班级只有一个班主任
  • 2、Windows 是多进程多线程的,在操作一个文件的时候,就不可避免地出现多个进程或线程同时操作一个文件的现象,所以所有文件的处理必须通过唯一的实例来进行。
  • 3、一些设备管理器常常设计为单例模式,比如一个电脑有两台打印机,在输出的时候就要处理不能两台打印机打印同一个文件。

同样,我们在src文件夹下创建一个singleton文件夹,然后在其文件夹下分别在创建两个文件crawler1.ts和urlAnalyzer.ts。

这两个文件的作用与上文同样,只不过代码书写不一样。

crawler1.ts

  1. import superagent from “superagent”;
  2. import fs from “fs”;
  3. import path from “path”;
  4. import UrlAnalyzer from “./urlAnalyzer.ts”;
  5. export interface Analyzer {
  6.   analyze: (html: string, filePath: string) => string;
  7. }
  8. class Crowller {
  9.   private filePath = path.resolve(__dirname, “../../data/url.json”);
  10.   async getRawHtml() {
  11.     const result = await superagent.get(this.url);
  12.     return result.text;
  13.   }
  14.   private writeFile(content: string) {
  15.     fs.writeFileSync(this.filePath, content);
  16.   }
  17.   private async initSpiderProcess() {
  18.     const html = await this.getRawHtml();
  19.     const fileContent = this.analyzer.analyze(html, this.filePath);
  20.     this.writeFile(JSON.stringify(fileContent));
  21.   }
  22.   constructor(private analyzer: Analyzer, private url: string) {
  23.     this.initSpiderProcess();
  24.   }
  25. }
  26. const url = “https://www.hanju.run/play/39257-1-1.html”;
  27. const analyzer = UrlAnalyzer.getInstance();
  28. new Crowller(analyzer, url);

urlAnalyzer.ts

  1. import cheerio from “cheerio”;
  2. import fs from “fs”;
  3. import { Analyzer } from “./crawler1.ts”;
  4. interface objJson {
  5.   [propName: number]: Info[];
  6. }
  7. interface InfoResult {
  8.   time: number;
  9.   data: Info[];
  10. }
  11. interface Info {
  12.   name: string;
  13.   url: string;
  14. }
  15. export default class UrlAnalyzer implements Analyzer {
  16.   static instance: UrlAnalyzer;
  17.   static getInstance() {
  18.     if (!UrlAnalyzer.instance) {
  19.       UrlAnalyzer.instance = new UrlAnalyzer();
  20.     }
  21.     return UrlAnalyzer.instance;
  22.   }
  23.   private getJsonInfo(html: string) {
  24.     const $ = cheerio.load(html);
  25.     const info: Info[] = [];
  26.     const scpt: string = String($(“.play>script:nth-child(1)”).html());
  27.     const url = unescape(
  28.       scpt.split(“;”)[3].split(“(“)[1].split(“)”)[0].replace(/”/g, “”)
  29.     );
  30.     const name: string = String($(“title”).html());
  31.     info.push({
  32.       name,
  33.       url,
  34.     });
  35.     const result = {
  36.       time: new Date().getTime(),
  37.       data: info,
  38.     };
  39.     return result;
  40.   }
  41.   private getJsonContent(info: InfoResult, filePath: string) {
  42.     let fileContent: objJson = {};
  43.     if (fs.existsSync(filePath)) {
  44.       fileContent = JSON.parse(fs.readFileSync(filePath, “utf-8”));
  45.     }
  46.     fileContent[info.time] = info.data;
  47.     return fileContent;
  48.   }
  49.   public analyze(html: string, filePath: string) {
  50.      const info = this.getJsonInfo(html);
  51.      console.log(info);
  52.     const fileContent = this.getJsonContent(info, filePath);
  53.     return JSON.stringify(fileContent);
  54.   }
  55.   private constructor() {}
  56. }

可以在package.json文件中定义快捷启动命令。

  1. “scripts”: {
  2.     “dev-s”: “ts-node ./src/singleton/crawler1.ts”,
  3.  },

然后使用npm run dev-s启动即可。

结语

这下真的结束了,谢谢阅读。希望可以帮到你。

完整源码地址:

https://github.com/maomincoding/TsCrawler

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发

请登录后发表评论