nodejs express搭建服务器(爬虫知乎精华帖,个人学习用)四 存储提到的内容的次数

var cheerio = require('cheerio');
var iconv = require('iconv-lite');
var https= require('https');
var mysql = require('mysql');

// 创建连接
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : '12345678',
    database : 'testmysql'
});

connection.connect();

var bookList = [];
var Bindex = 0;

function zhihuin(Bindex, index) {
    console.log(Bindex);
    return new Promise(function (resolve, reject) {
        var  addSql = 'INSERT INTO zhihuNum(Id, name, num) VALUES(?,?,?)';
        var  addSqlParams = [Bindex, bookList[index].name, bookList[index].num];
        // 这个连接也是异步的
        connection.query(addSql, addSqlParams,function (err, result) {
            if(err){
                console.log('[INSERT ERROR] - ',err.message);
                reject(0);
            }else {
                resolve(1);
            }
        });
    })
}

function zhihuinnum(Bindex, index) {
    zhihuin(Bindex, index).then(function (data) {
        Bindex++;
        index++;
        if(index < bookList.length){
            zhihuinnum(Bindex, index);
        }else {
            connection.end();
        }
    },function (data) {
        index++;
        if(index < bookList.length){
            zhihuinnum(Bindex, index);
        }else {
            connection.end();
        }
    })
}

function zhihunum(){
    var  sql = 'SELECT works FROM zhihuBook';
//查
    connection.query(sql,function (err, result) {
        if(err){
            console.log('[SELECT ERROR] - ',err.message);
            return;
        }else {
            for(let item of result){
                if(item.works != ''){
                    // console.log(item.works.trim().split(/\s+/));
                    var bookwork = item.works.match(/《.*?》/g);
                    // console.log(bookwork)
                    for (let ilist of bookwork){
                        if(bookList.length == 0){
                            bookList.push({'name': ilist, 'num': 1});
                        }else {
                            var i = 0;
                            for(i = 0; i < bookList.length; i++){
                                if(ilist == bookList[i].name){
                                    console.log(ilist, bookList[i].name)
                                    bookList[i].num++;
                                    break;
                                }
                            }
                            if(i >= bookList.length){
                                bookList.push({'name': ilist, 'num': 1});
                            }
                        }
                    }
                }
            }
            // console.log(bookList);
            bookList = bookList.sort((x, y)=>{
                if(x.num < y.num){
                    return 1;
                }else if(x.num > y.num){
                    return  -1;
                }else{
                    return 0;
                }
            });
            zhihuinnum(0, 0);
        }
    });
}

zhihunum();
// connection.end();


1、数组的排序函数sort

bookList = bookList.sort((x, y)=>{
                if(x.num < y.num){
                    return 1;
                }else if(x.num > y.num){
                    return  -1;
                }else{
                    return 0;
                }
            });

接受两个参数,都是数组中的元素,第二个参数在数组中的序列是在第一个参数后面,函数体中,返回正数,表明两个元素交换位置,返回0,表明相等。返回负数,表明不交换位置。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值