• 【Nodejs】外研社一年级起三年级下MP3下载爬虫1.00


    //======================================================
    // 外研社一年级起三年级下MP3下载爬虫1.00
    // 2018年5月9日
    //======================================================
    var http=require("http");            // http模块
    var https=require("https");            // https模块
    var zlib = require('zlib');            // 用于解析gzip网页
    var fs=require('fs');                // 文件处理模块
    var iconv = require('iconv-lite');    // 用于转码。
    var cheerio = require("cheerio");    // 用于从HTML中以类似jquery方式查找目标
    
    //-------------------------------
    // 找到页码里attachid 和 title
    // pageUrl:页码地址
    //-------------------------------
    function findAttachidTitle(pageUrl){
        console.log("page="+pageUrl);
    
        var currUrl=pageUrl.replace("https://","");
        var pos=currUrl.indexOf("/");
        var hostname=currUrl.slice(0,pos);    
        var path=currUrl.slice(pos);    
        pos=currUrl.lastIndexOf("/");
        var dir="https://"+currUrl.slice(0,pos);            
        
        var options={
            hostname:hostname,
                port:443,
                path:path,
              method:'GET',        
        };    
    
        var req=https.request(options,function(resp){
            var html = [];
    
            resp.on("data", function(data) {
                html.push(data);
            })
            resp.on("end", function() {
                var buffer = Buffer.concat(html);
                var body = iconv.decode(buffer,'gb2312');
                var $ = cheerio.load(body);            
    
                var arr=[];
                $(".clearfix li a").each(function(index,element){
                    var attachid=$(element).attr("attachid");
                    var title=$(element).attr("title");
    
                    var regexp=/^(d+)$/;
    
                    if(regexp.test(attachid)==true){
                        console.log("attachid="+attachid+" title="+title);
                        
                        var item={'attachid':attachid,'title':title};
                        arr.push(item);
                    }
                })  
    
                download(arr);
            }).on("error", function(err) {
                console.log("findAttachidTitle函数请求后获取响应时出现异常"+err);
            })
        });
    
        // 超时处理
        req.setTimeout(7500,function(){
            req.abort();
        });
    
        // 出错处理
        req.on('error',function(err){
            console.log('findAttachidTitle函数请求时发生错误'+err);  
        });
    
        // 请求结束
        req.end();    
    }
    
    //-------------------------------
    // 批量下载
    // arr:包含attachid和title的数组
    //-------------------------------
    function download(arr){
        var total=arr.length;
        console.log("总计有"+total+"个文件将被下载.");
    
        for(var i=0;i<arr.length;i++){
            var item=arr[i];        
            var mp3url=findMp3Url(item.attachid);
            if(mp3url!=null){
                //console.log(mp3url);
                downloadMp3(mp3url,item.title);
            }
        }
    }
    
    //-------------------------------
    // 按attachid,title找到对应的MP3地址
    //-------------------------------
    function findMp3Url(attachid){
        // 这个是从原网页jS代码里拷贝出来的
        var list = [{"id":"5237854","midID":"973986","itemid":"2117465","dateline":"1503415079","filetype":".mp3","fileorder":"0","filesize":"832592","attachment":"http://preview.21cnjy.net/files/03/3f/033fd8156a3c679538a4adabfe45d1ae.mp3","isimage":"0","cmd5":"c1473a344905e759b56943a117aeb312","uversion":"0"},{"id":"5237855","midID":"973986","itemid":"2117465","dateline":"1503415079","filetype":".ogg","fileorder":"0","filesize":"751378","attachment":"http://preview.21cnjy.net/files/6c/d3/6cd381b2c14fe3f77b53eccb87538d6e.ogg","isimage":"0","cmd5":"0a9918ed4118c01e03f273b78d27b413","uversion":"0"},{"id":"5237856","midID":"973993","itemid":"2117465","dateline":"1503415079","filetype":".mp3","fileorder":"0","filesize":"960488","attachment":"http://preview.21cnjy.net/files/5d/0b/5d0b582cf3739209543d74c053a824e1.mp3","isimage":"0","cmd5":"8b3642655ad7eb5966577cbfe1ae0f89","uversion":"0"},{"id":"5237857","midID":"973993","itemid":"2117465","dateline":"1503415079","filetype":".ogg","fileorder":"0","filesize":"901991","attachment":"http://preview.21cnjy.net/files/c5/3b/c53b904fde39312751b168c6e52de6e7.ogg","isimage":"0","cmd5":"df2666cec05592e43ff63364d358f047","uversion":"0"},{"id":"5237858","midID":"973998","itemid":"2117465","dateline":"1503415079","filetype":".mp3","fileorder":"0","filesize":"572204","attachment":"http://preview.21cnjy.net/files/7d/72/7d72d5239dafe2e0c00108885c4fb65d.mp3","isimage":"0","cmd5":"18d3f6c870ff81f26d8789a0f5d2f9d0","uversion":"0"},{"id":"5237859","midID":"973998","itemid":"2117465","dateline":"1503415079","filetype":".ogg","fileorder":"0","filesize":"256372","attachment":"http://preview.21cnjy.net/files/b4/42/b4429006dedd246d80efefa4629bedc6.ogg","isimage":"0","cmd5":"f74116900d35b2e37db4a219ca3d49c3","uversion":"0"},{"id":"5237860","midID":"974001","itemid":"2117465","dateline":"1503415079","filetype":".mp3","fileorder":"0","filesize":"960488","attachment":"http://preview.21cnjy.net/files/ca/c5/cac5f315b00445d05c5b50c0e8e83d84.mp3","isimage":"0","cmd5":"d48d05dc2609c362edb39a2c8a35d9a5","uversion":"0"},{"id":"5237861","midID":"974001","itemid":"2117465","dateline":"1503415080","filetype":".ogg","fileorder":"0","filesize":"899020","attachment":"http://preview.21cnjy.net/files/eb/ba/ebbac8801fdd682c4f1b65bb3d198c02.ogg","isimage":"0","cmd5":"e835a9fa38b81ac47a1535cea8a0244b","uversion":"0"},{"id":"5237862","midID":"974006","itemid":"2117465","dateline":"1503415080","filetype":".mp3","fileorder":"0","filesize":"960488","attachment":"http://preview.21cnjy.net/files/e6/60/e660a2d2b2a2f0ba9c237d547b85c76c.mp3","isimage":"0","cmd5":"db55d15aef4a5ae8a71926ad9d24662a","uversion":"0"},{"id":"5237864","midID":"974006","itemid":"2117465","dateline":"1503415080","filetype":".ogg","fileorder":"0","filesize":"900591","attachment":"http://preview.21cnjy.net/files/3e/ae/3eae173f4af01593b5dd4d2efbcbad93.ogg","isimage":"0","cmd5":"522c77272d5c22ca8330d8d166836da0","uversion":"0"},{"id":"5237866","midID":"974011","itemid":"2117465","dateline":"1503415080","filetype":".mp3","fileorder":"0","filesize":"463534","attachment":"http://preview.21cnjy.net/files/e9/a3/e9a3d806cf951e0e5db88a319ce39f44.mp3","isimage":"0","cmd5":"528fcdb226d36b8425d57ef2aa95cd6c","uversion":"0"},{"id":"5237868","midID":"974011","itemid":"2117465","dateline":"1503415080","filetype":".ogg","fileorder":"0","filesize":"214655","attachment":"http://preview.21cnjy.net/files/84/ee/84ee8204a8abe2ce0f975e24c8b25afc.ogg","isimage":"0","cmd5":"7067629e0b0a0493e45c3459edc2f632","uversion":"0"},{"id":"5237870","midID":"974014","itemid":"2117465","dateline":"1503415080","filetype":".mp3","fileorder":"0","filesize":"960488","attachment":"http://preview.21cnjy.net/files/9b/88/9b88337a54b299ed8f2f7b9545ed90e2.mp3","isimage":"0","cmd5":"ae9cc42c2cddfffb5fb10181368c0c27","uversion":"0"},{"id":"5237871","midID":"974014","itemid":"2117465","dateline":"1503415080","filetype":".ogg","fileorder":"0","filesize":"812025","attachment":"http://preview.21cnjy.net/files/a6/bd/a6bd502d2a37408087014e590fc136b8.ogg","isimage":"0","cmd5":"5bf4ba1fd9a32e733dcdc4f95cded83d","uversion":"0"},{"id":"5237872","midID":"974019","itemid":"2117465","dateline":"1503415081","filetype":".mp3","fileorder":"0","filesize":"960488","attachment":"http://preview.21cnjy.net/files/80/3e/803e3fb66cf002395c07519551818916.mp3","isimage":"0","cmd5":"df2e91190bf74a0f1d6da40942b0cf37","uversion":"0"},{"id":"5237873","midID":"974019","itemid":"2117465","dateline":"1503415081","filetype":".ogg","fileorder":"0","filesize":"910560","attachment":"http://preview.21cnjy.net/files/cc/36/cc36033233b2ede4b460df69a5835e5b.ogg","isimage":"0","cmd5":"dd90b8cddf05f8c5973052f69c39bb6f","uversion":"0"},{"id":"5237874","midID":"974025","itemid":"2117465","dateline":"1503415081","filetype":".mp3","fileorder":"0","filesize":"430307","attachment":"http://preview.21cnjy.net/files/92/3a/923ab19bdab65cc373859731cf1f4bb4.mp3","isimage":"0","cmd5":"37e8eb966a0e8adfd485a02a72410f37","uversion":"0"},{"id":"5237875","midID":"974025","itemid":"2117465","dateline":"1503415081","filetype":".ogg","fileorder":"0","filesize":"225427","attachment":"http://preview.21cnjy.net/files/0d/22/0d22bfabb6662d18a9e82cd559128b38.ogg","isimage":"0","cmd5":"063604a42e288e53f00f214c143795af","uversion":"0"},{"id":"5237876","midID":"974027","itemid":"2117465","dateline":"1503415081","filetype":".mp3","fileorder":"0","filesize":"960488","attachment":"http://preview.21cnjy.net/files/53/1c/531ca9ed1b51e1bef472a730bb418a02.mp3","isimage":"0","cmd5":"ddb5fdbeebb85bfa45aa2592cc60660c","uversion":"0"},{"id":"5237879","midID":"974027","itemid":"2117465","dateline":"1503415081","filetype":".ogg","fileorder":"0","filesize":"891908","attachment":"http://preview.21cnjy.net/files/ea/f2/eaf23dddd4ead7f7c7a30c5f2b284ed0.ogg","isimage":"0","cmd5":"4410ee2b725ce13e245815880b9f9a02","uversion":"0"},{"id":"5237881","midID":"974030","itemid":"2117465","dateline":"1503415081","filetype":".mp3","fileorder":"0","filesize":"960488","attachment":"http://preview.21cnjy.net/files/93/43/934359e6f976f10a343cd0a362105d02.mp3","isimage":"0","cmd5":"d297fc17cf1387888103ed1a49c3e995","uversion":"0"},{"id":"5237883","midID":"974030","itemid":"2117465","dateline":"1503415082","filetype":".ogg","fileorder":"0","filesize":"851148","attachment":"http://preview.21cnjy.net/files/a3/5b/a35b01617268bd1619b14d728f099e7e.ogg","isimage":"0","cmd5":"cd33a1a591f781787daafa3142939733","uversion":"0"},{"id":"5237884","midID":"974034","itemid":"2117465","dateline":"1503415082","filetype":".mp3","fileorder":"0","filesize":"453294","attachment":"http://preview.21cnjy.net/files/94/c7/94c75e3da7e37a5af6f424c0c7288781.mp3","isimage":"0","cmd5":"6c7a5f3166337a844a91f022f5053a3c","uversion":"0"},{"id":"5237885","midID":"974034","itemid":"2117465","dateline":"1503415082","filetype":".ogg","fileorder":"0","filesize":"245896","attachment":"http://preview.21cnjy.net/files/15/40/1540a685363823a4ad6e939a2ba2dade.ogg","isimage":"0","cmd5":"a42629e5ecf5f9c7330321ab341d794a","uversion":"0"},{"id":"5237886","midID":"974036","itemid":"2117465","dateline":"1503415082","filetype":".mp3","fileorder":"0","filesize":"910751","attachment":"http://preview.21cnjy.net/files/13/9d/139d8e2b95df89b89224c3787ce77907.mp3","isimage":"0","cmd5":"d3d0f064eb78ba6e3a4c6d2d48ee3fd2","uversion":"0"},{"id":"5237887","midID":"974036","itemid":"2117465","dateline":"1503415082","filetype":".ogg","fileorder":"0","filesize":"839263","attachment":"http://preview.21cnjy.net/files/c6/8f/c68f52ceafbdad4776de5936a22096e5.ogg","isimage":"0","cmd5":"56e1f077b8079741eb1888f618d18f6a","uversion":"0"},{"id":"5237888","midID":"974039","itemid":"2117465","dateline":"1503415082","filetype":".mp3","fileorder":"0","filesize":"960488","attachment":"http://preview.21cnjy.net/files/91/25/9125e55f458f8122dd46c6bf76226d3e.mp3","isimage":"0","cmd5":"bbd908587d8d79c470ca441cc964ec08","uversion":"0"},{"id":"5237889","midID":"974039","itemid":"2117465","dateline":"1503415082","filetype":".ogg","fileorder":"0","filesize":"901958","attachment":"http://preview.21cnjy.net/files/04/d2/04d2f7f5e641f52863a8a9df9ff12c53.ogg","isimage":"0","cmd5":"34bf883e28fd52db686c3a8f8d8c169c","uversion":"0"},{"id":"5237890","midID":"974044","itemid":"2117465","dateline":"1503415083","filetype":".mp3","fileorder":"0","filesize":"321846","attachment":"http://preview.21cnjy.net/files/a7/bf/a7bf51adb4e11acd2bb6452a32079e71.mp3","isimage":"0","cmd5":"e3600f97c9297be5d4c97ac3741247a3","uversion":"0"},{"id":"5237891","midID":"974044","itemid":"2117465","dateline":"1503415083","filetype":".ogg","fileorder":"0","filesize":"175330","attachment":"http://preview.21cnjy.net/files/5f/4f/5f4fa9ae133ee07afd9b99e10365068f.ogg","isimage":"0","cmd5":"39f9a137a87c6ad898f2fa4584983a08","uversion":"0"},{"id":"5237892","midID":"974045","itemid":"2117465","dateline":"1503415083","filetype":".mp3","fileorder":"0","filesize":"960488","attachment":"http://preview.21cnjy.net/files/8e/5c/8e5cbf783d25ce3ed8c530804efc057f.mp3","isimage":"0","cmd5":"6d73d53e1d8b1d1fd3ad859d3749e842","uversion":"0"},{"id":"5237893","midID":"974045","itemid":"2117465","dateline":"1503415083","filetype":".ogg","fileorder":"0","filesize":"904258","attachment":"http://preview.21cnjy.net/files/25/9b/259bd36bbd59a4f91cc3c4fb54f51d08.ogg","isimage":"0","cmd5":"acbab9cd8e6f635b45481ab67d619860","uversion":"0"},{"id":"5237894","midID":"974047","itemid":"2117465","dateline":"1503415083","filetype":".mp3","fileorder":"0","filesize":"960488","attachment":"http://preview.21cnjy.net/files/db/46/db46f244966a4dc5f1be47d41159479a.mp3","isimage":"0","cmd5":"4b0b531276d7778700afcac00b01b318","uversion":"0"},{"id":"5237895","midID":"974047","itemid":"2117465","dateline":"1503415083","filetype":".ogg","fileorder":"0","filesize":"909713","attachment":"http://preview.21cnjy.net/files/59/3f/593f71bff264a8db795f8f87166bc224.ogg","isimage":"0","cmd5":"7164117fe7b5be4e811c1bf94cf05ee9","uversion":"0"},{"id":"5237896","midID":"974049","itemid":"2117465","dateline":"1503415083","filetype":".mp3","fileorder":"0","filesize":"157797","attachment":"http://preview.21cnjy.net/files/45/df/45df939375ec3bf31525829f941dd047.mp3","isimage":"0","cmd5":"32fb7f3e5a35829cd9e0332d5d37f165","uversion":"0"},{"id":"5237897","midID":"974049","itemid":"2117465","dateline":"1503415083","filetype":".ogg","fileorder":"0","filesize":"104738","attachment":"http://preview.21cnjy.net/files/86/e9/86e922ec780cf966a6d4b77029aa1292.ogg","isimage":"0","cmd5":"e45b1daa6d0fd0f4a37e0cc1ba4cc7d6","uversion":"0"},{"id":"5237898","midID":"974051","itemid":"2117465","dateline":"1503415083","filetype":".mp3","fileorder":"0","filesize":"960488","attachment":"http://preview.21cnjy.net/files/a4/a5/a4a545b4105cc76dc943a7d07335f557.mp3","isimage":"0","cmd5":"cdd71ef18745c5b649503defbbe564ad","uversion":"0"},{"id":"5237899","midID":"974051","itemid":"2117465","dateline":"1503415083","filetype":".ogg","fileorder":"0","filesize":"896444","attachment":"http://preview.21cnjy.net/files/ad/1c/ad1c628341a70ac224e18adaece819f3.ogg","isimage":"0","cmd5":"e14316e2bd00041349173bda7665ae3e","uversion":"0"},{"id":"5237900","midID":"974054","itemid":"2117465","dateline":"1503415083","filetype":".mp3","fileorder":"0","filesize":"960488","attachment":"http://preview.21cnjy.net/files/43/98/439866e77582b9f71e3fcf84defc436c.mp3","isimage":"0","cmd5":"cc4efc54a353629e89f39045c1a16551","uversion":"0"},{"id":"5237901","midID":"974054","itemid":"2117465","dateline":"1503415084","filetype":".ogg","fileorder":"0","filesize":"909621","attachment":"http://preview.21cnjy.net/files/f9/e4/f9e48399c1961861d98a2c12c968344f.ogg","isimage":"0","cmd5":"323e76eb01cba93c40de682362bd1d20","uversion":"0"},{"id":"5237902","midID":"974058","itemid":"2117465","dateline":"1503415084","filetype":".mp3","fileorder":"0","filesize":"282140","attachment":"http://preview.21cnjy.net/files/db/8d/db8dfaf1712988ed0c3672f4a93905a4.mp3","isimage":"0","cmd5":"13ffc32d1e43dead0aecc567034b2728","uversion":"0"},{"id":"5237903","midID":"974058","itemid":"2117465","dateline":"1503415084","filetype":".ogg","fileorder":"0","filesize":"155967","attachment":"http://preview.21cnjy.net/files/c6/e2/c6e2cffb6cb05ae2d0fa4bd27910d6a5.ogg","isimage":"0","cmd5":"0e0017fd3064556b26fc3be0e84aaef1","uversion":"0"},{"id":"5237904","midID":"974060","itemid":"2117465","dateline":"1503415084","filetype":".mp3","fileorder":"0","filesize":"945232","attachment":"http://preview.21cnjy.net/files/4e/83/4e8336a5de886638fb6e481c7d05a985.mp3","isimage":"0","cmd5":"1cc9fbc8f70616169925537277c62337","uversion":"0"},{"id":"5237905","midID":"974060","itemid":"2117465","dateline":"1503415084","filetype":".ogg","fileorder":"0","filesize":"869672","attachment":"http://preview.21cnjy.net/files/ae/48/ae483bd340438b285b5730870c9da014.ogg","isimage":"0","cmd5":"18d8f279f8e6c6b51b4f6047394cd7ac","uversion":"0"},{"id":"5237906","midID":"974065","itemid":"2117465","dateline":"1503415084","filetype":".mp3","fileorder":"0","filesize":"960488","attachment":"http://preview.21cnjy.net/files/88/7a/887aa3673107c09f2777e4c8dfc31d23.mp3","isimage":"0","cmd5":"3358b81c144158f8cb439574dc30c964","uversion":"0"},{"id":"5237907","midID":"974065","itemid":"2117465","dateline":"1503415084","filetype":".ogg","fileorder":"0","filesize":"897395","attachment":"http://preview.21cnjy.net/files/1d/a7/1da7100ed60b2620878df102f8f5c40b.ogg","isimage":"0","cmd5":"d494ad22532a1468596d7ca440c77961","uversion":"0"},{"id":"5237908","midID":"974070","itemid":"2117465","dateline":"1503415084","filetype":".mp3","fileorder":"0","filesize":"509092","attachment":"http://preview.21cnjy.net/files/57/b4/57b400667759f940cb90af4f63ebd50a.mp3","isimage":"0","cmd5":"4af7cf95b8d61e9268e74fae3657f5ea","uversion":"0"},{"id":"5237909","midID":"974070","itemid":"2117465","dateline":"1503415084","filetype":".ogg","fileorder":"0","filesize":"284790","attachment":"http://preview.21cnjy.net/files/52/b4/52b43e12377254caa47a5cb3b0b18089.ogg","isimage":"0","cmd5":"8aa8978ddf5c3074ed16b3b49e60747f","uversion":"0"},{"id":"5237910","midID":"974072","itemid":"2117465","dateline":"1503415085","filetype":".mp3","fileorder":"0","filesize":"872298","attachment":"http://preview.21cnjy.net/files/49/b8/49b85eb866ad3aed7244e6b2bb83687c.mp3","isimage":"0","cmd5":"435f9d77cea40a4dfb56d6854e213425","uversion":"0"},{"id":"5237911","midID":"974072","itemid":"2117465","dateline":"1503415085","filetype":".ogg","fileorder":"0","filesize":"803574","attachment":"http://preview.21cnjy.net/files/bd/ec/bdec15b08fe6cb633074a25b7a256fab.ogg","isimage":"0","cmd5":"e6c16dddb09abebc432983c00f9f2dc9","uversion":"0"},{"id":"5237912","midID":"974074","itemid":"2117465","dateline":"1503415085","filetype":".mp3","fileorder":"0","filesize":"960488","attachment":"http://preview.21cnjy.net/files/99/f2/99f27898182ba3498f73a944c803efd1.mp3","isimage":"0","cmd5":"fa6f0f492bc6824777c363c14e69a9d7","uversion":"0"},{"id":"5237913","midID":"974074","itemid":"2117465","dateline":"1503415085","filetype":".ogg","fileorder":"0","filesize":"874111","attachment":"http://preview.21cnjy.net/files/19/0c/190c1bd584009d14f9d2fbae4ef2ed42.ogg","isimage":"0","cmd5":"ae83e04bfa6ddebf9055f5e34249b75b","uversion":"0"},{"id":"5237914","midID":"974079","itemid":"2117465","dateline":"1503415085","filetype":".mp3","fileorder":"0","filesize":"507211","attachment":"http://preview.21cnjy.net/files/24/de/24deab6027196bc5672a06769a5cc73a.mp3","isimage":"0","cmd5":"e09e755dd53497fd685039a43f5cee1d","uversion":"0"},{"id":"5237915","midID":"974079","itemid":"2117465","dateline":"1503415085","filetype":".ogg","fileorder":"0","filesize":"287327","attachment":"http://preview.21cnjy.net/files/4b/cd/4bcd68a9877ac6a616599debdcbf1826.ogg","isimage":"0","cmd5":"840a2051a34d58c5812cd5ea7690c1f9","uversion":"0"},{"id":"5237916","midID":"974081","itemid":"2117465","dateline":"1503415085","filetype":".mp3","fileorder":"0","filesize":"960488","attachment":"http://preview.21cnjy.net/files/6f/d8/6fd8f7453ef5c0ef4836af7b497cd8a2.mp3","isimage":"0","cmd5":"7471b4470cd615aaf01c2cba8fee6e9a","uversion":"0"},{"id":"5237917","midID":"974081","itemid":"2117465","dateline":"1503415085","filetype":".ogg","fileorder":"0","filesize":"828958","attachment":"http://preview.21cnjy.net/files/2b/7b/2b7b214fc472393179e04e8c53b32401.ogg","isimage":"0","cmd5":"b826aa4ba0e9ab6679bb32e45c719cff","uversion":"0"},{"id":"5237919","midID":"974085","itemid":"2117465","dateline":"1503415085","filetype":".mp3","fileorder":"0","filesize":"960488","attachment":"http://preview.21cnjy.net/files/75/cc/75cc8518c4334c1ad6dd0d24773a9888.mp3","isimage":"0","cmd5":"36157dc72a9af21c600f589d0894b3cb","uversion":"0"},{"id":"5237923","midID":"974085","itemid":"2117465","dateline":"1503415085","filetype":".ogg","fileorder":"0","filesize":"876486","attachment":"http://preview.21cnjy.net/files/f4/cc/f4ccfc3b928db08fcc03d0d5b4966495.ogg","isimage":"0","cmd5":"b5e0af45d50b1154c1313bbafc90aa67","uversion":"0"},{"id":"5237924","midID":"974088","itemid":"2117465","dateline":"1503415085","filetype":".mp3","fileorder":"0","filesize":"323727","attachment":"http://preview.21cnjy.net/files/c2/6f/c26fb32331acda77b139184f8e8a23e6.mp3","isimage":"0","cmd5":"8e94959569f2808df2973602b06cb6cb","uversion":"0"},{"id":"5237925","midID":"974088","itemid":"2117465","dateline":"1503415085","filetype":".ogg","fileorder":"0","filesize":"179155","attachment":"http://preview.21cnjy.net/files/d8/fc/d8fc08a593a3188c64763099958852ee.ogg","isimage":"0","cmd5":"ea051866958ba9e3d68ca81df8fa80dd","uversion":"0"},{"id":"5237926","midID":"974090","itemid":"2117465","dateline":"1503415086","filetype":".mp3","fileorder":"0","filesize":"466251","attachment":"http://preview.21cnjy.net/files/c6/14/c6140f9881e6343b38d0facded640422.mp3","isimage":"0","cmd5":"a8d38c6dd8cfeb84e61d55b68c17cb77","uversion":"0"},{"id":"5237927","midID":"974090","itemid":"2117465","dateline":"1503415086","filetype":".ogg","fileorder":"0","filesize":"413297","attachment":"http://preview.21cnjy.net/files/e8/1a/e81afd54c418aee0605432e23f09cf46.ogg","isimage":"0","cmd5":"2ee659c7f19c4795462343b0107815d2","uversion":"0"},{"id":"5237928","midID":"974094","itemid":"2117465","dateline":"1503415086","filetype":".mp3","fileorder":"0","filesize":"926633","attachment":"http://preview.21cnjy.net/files/fb/1e/fb1ed742a520a812d4311856a6fed51e.mp3","isimage":"0","cmd5":"281f4ab7ef17a340b4068116a8e2e178","uversion":"0"},{"id":"5237929","midID":"974094","itemid":"2117465","dateline":"1503415086","filetype":".ogg","fileorder":"0","filesize":"877362","attachment":"http://preview.21cnjy.net/files/e4/2c/e42c7da54e295de755854f04a2bd1ab7.ogg","isimage":"0","cmd5":"90e47d2ec304497dc8ea5eacb162b01f","uversion":"0"}];
    
        for(var i=0;i<list.length;i++){
            var item=list[i];
            
            if(item.midID==attachid && item.attachment.endsWith('.mp3')){
                var mp3url=item.attachment;
                mp3url=mp3url.replace('\','')
                return mp3url;
            }
        }
    
        return null;
    }
    
    //--------------------------------------
    // 下载单个mp3
    // picUrl sample:http://preview.21cnjy.net/files/03/3f/033fd8156a3c679538a4adabfe45d1ae.mp3
    //--------------------------------------
    function downloadMp3(url,title){
        console.log("mp3:"+url+"下载开始");
    
        // 得到hostname,path和port
        var currUrl=url.replace("http://","");
        var pos=currUrl.indexOf("/");
        var hostname=currUrl.slice(0,pos);        
        var path=currUrl.slice(pos);
    
        // 有端口加端口,没有端口默认80
        var port=80;
        if(hostname.indexOf(":")!=-1){
            var arr=hostname.split(":");
            hostname=arr[0];
            port=arr[1];
        }
    
        //console.log("hostname="+hostname);
        //console.log("path="+path);
        //console.log("port="+port);
    
        var name=title+".mp3"
        
        // 初始化options  
        options={
            hostname:hostname,
                port:port,
                path:path,
              method:'GET',
            /* headers:{
                'Referer':'https://www.nvshens.com',
              },*/  // 有需要再打开
        };
    
        req=http.request(options,function(resp){
            var imgData = "";
            resp.setEncoding("binary"); 
    
            resp.on('data',function(chunk){
                imgData+=chunk;            
            });
    
            resp.on('end',function(){        
    
                // 创建文件
                var fileName="./"+name;
                fs.writeFile(fileName, imgData, "binary", function(err){
                    if(err){
                        console.log("[downloadPic]文件   "+fileName+"  下载失败.");
                        console.log(err);
                    }else{
                        console.log("文件"+fileName+"下载成功");
                    }
                });    
            });
        });
    
        // 超时处理
        req.setTimeout(7500,function(){
            req.abort();
        });
    
        // 出错处理
        req.on('error',function(err){
            if(err){
                console.log('[downloadMp3]文件   '+url+"  下载失败,"+'因为'+err);
            }
        });
    
        // 请求结束
        req.end();
    }
    
    //--------------------------------------
    // 通用函数,返回当前日期时间 控制台输出时间用
    //--------------------------------------
    function nowTime() {
        var date = new Date();
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate =date.getFullYear() + seperator1 + month + seperator1 + strDate
                + " " + date.getHours() + seperator2 + date.getMinutes()
                + seperator2 + date.getSeconds()+ " ";
        return currentdate;
    }
    
    //-------------------------------
    // 得到带颜色(前景色)的文字,用于在控制台输出
    // text:文字,color:前景色
    //-------------------------------
    function coloredText(text,color){
        var dic = new Array();
    
        dic["white"] = ['x1B[37m', 'x1B[39m'];
        dic["grey"] = ['x1B[90m', 'x1B[39m'];
        dic["black"] = ['x1B[30m', 'x1B[39m'];
        dic["blue"] = ['x1B[34m', 'x1B[39m'];
        dic["cyan"] = ['x1B[36m', 'x1B[39m'];
        dic["green"] = ['x1B[32m', 'x1B[39m'];
        dic["magenta"] = ['x1B[35m', 'x1B[39m'];
        dic["red"] = ['x1B[31m', 'x1B[39m'];
        dic["yellow"] = ['x1B[33m', 'x1B[39m'];
    
        return dic[color][0]+text+dic[color][1];
    }
    
    //-------------------------------
    // 入口函数
    //-------------------------------
    function main(){
         findAttachidTitle('https://www.21cnjy.com/H/4/46506/2117465.shtml');
    }
    
    // 开始
    main();

    下载的结果共享给大家:https://pan.baidu.com/s/1wd8S7Ay9DIWrdWGi3zre2Q

    2018年5月9日21点48分

    补记:

    外研社小学英语MP3全集下载地址:https://pan.baidu.com/s/19hJ59SsX9uTfFtBxaRxJ8A

    2018年5月13日

  • 相关阅读:
    数据分析 第五篇:离群点检测
    linux lsof命令详解
    ES基本查询语句教程
    Swagger详解(SpringBoot+Swagger集成)
    Elasticsearch5.0 安装问题集锦
    kafka auto.offset.reset latest earliest 详解
    干货 | Elasticsearch多表关联设计指南
    kafka auto.offset.reset latest earliest 详解
    linux sed命令
    Idea中如何使用debug操作
  • 原文地址:https://www.cnblogs.com/heyang78/p/9016885.html
Copyright © 2020-2023  润新知