学习篇-TypeScript-爬虫-基本实现

一、TypeScript-爬虫-基本实现
  • 示例代码

    class Crowller {
      // 定义需要爬虫的url链接
      private url: string =
        "https://learning.sohu.com/?spm=smpc.news-home.header.7.1580904485627XJyFma3";
      // 定义处理好的数据保存路径
      private filePath = path.resolve(__dirname, "../data/news.json");
    
      /**
       * 处理html中的数据
       * @param html
       */
      getJsonInfo(html: string): string[] {
        const contents: string[] = [];
        const $ = cheerio.load(html);
        const lines = $(".z-head-news_item");
        lines.map((index, element) => {
          const childs = $(element).find("a");
          const content = childs.text();
          contents.push(content);
        });
        return contents;
      }
    
      /**
       * 获取网页内容
       */
      async getRawHtml() {
        const html = await superagent.get(this.url);
        return html.text;
      }
    
      /**
       * 读物文件内容
       * @param data
       */
      generateJsonContent(data: string[]) {
        let fileContent: Content = {};
        if (fs.existsSync(this.filePath)) {
          fileContent = JSON.parse(fs.readFileSync(this.filePath, "utf-8"));
        }
        let time = new Date().getTime();
        fileContent[time] = data;
        return fileContent;
      }
    
      /**
       * 将处理好的数据写入文件中
       * @param content
       */
      writeFile(content: string) {
        fs.writeFileSync(this.filePath, content);
      }
    
      /**
       * 1. 获取html
       * 2. 处理htmlz中的数据
       * 3. 获取文件中的内容
       * 4. 将处理好的数据写入文件中
       */
      async initSpiderRrocess() {
        const html = await this.getRawHtml();
        const jsonInfo = this.getJsonInfo(html);
        const content = this.generateJsonContent(jsonInfo);
        this.writeFile(JSON.stringify(content));
      }
    
      constructor() {
        this.initSpiderRrocess();
      }
    }
    
    const crowller = new Crowller();
    
    
    
    • 缺点:
      • 网页爬取和数据分析逻辑高度冗合
  • package.json配置

{
  "name": "demo-2",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "ts-node ./src/crowller.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cheerio": "^1.0.0-rc.3",
    "superagent": "^5.2.2"
  },
  "devDependencies": {
    "@types/cheerio": "^0.22.18",
    "@types/superagent": "^4.1.7"
  }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值