代码之家  ›  专栏  ›  技术社区  ›  Morton

如何拆分结果数?

  •  0
  • Morton  · 技术社区  · 8 年前

    我从网站上获得了电影时间数据,但我发现这是必然的结果。

    以下是我制作的json数据:

    [{"title":["敦克爾克大行動 Dunkirk","女狼嗨到趴 Rough Night","悟空傳 WuKong","猩球崛起 終極決戰 WAR FOR THE PLANET OF THE APES","銀魂 Gintama","蜘蛛人:返校日 Spider-Man: Homecoming","神偷奶爸 3《中文版》  Despicable Me 3 《 CH 》 ","神偷奶爸 3《英文版》  Despicable Me 3《 EN 》"],"time":["09:5011:5513:0014:0015:0016:0518:1019:2020:1522:2000:30\n","10:0011:5013:4015:3017:2020:5022:4000:30\n","10:0013:5516:1020:0522:2000:35\n","10:0012:3513:3515:1016:1017:4518:4520:2021:2022:5500:00\n","10:0017:0023:45\n","10:0012:2514:5017:1519:4021:2022:0500:30\n","10:0011:4519:10\n","12:1518:25\n"],"image":["picture/zip_20170617231635968.jpg","picture/zip_20170528225556605.jpg","picture/zip_20170528231038563.jpg","picture/zip_20170617225530468.jpg","picture/zip_20170617231105860.jpg","picture/zip_20170617224807245.jpg","picture/zip_20170617223724641.jpg","picture/zip_20170617223542725.jpg"]}]
    

    我希望时间变成这样: enter image description here

    我使用casperjs获取电影时间数据:

    var movieTitle = [];
    var movieTime = [];
    var movieImage = [];
    var allMovie = {};
    allMovie.detail = [];
    
    // How to let my movie time split out ?
    function getMovieTime() {
        var time = document.querySelectorAll('ul li ul');
        return Array.prototype.map.call(time, function (e) {
            return e.innerText;
        });
    }
    
    casper.then(function () {
        movieTitle = this.evaluate(getMovieTitle);
        movieTime = this.evaluate(getMovieTime);
        movieImage = this.evaluate(getMovieImage);
    });
    
    casper.then(function () {
        console.log('what is going on');
        this.echo(movieTitle.length + 'Title found :');
        // this.echo(movieTitle.join('\n'));
        this.echo(movieTime.length + 'Time found :');
        //this.echo(movieTime.join('\n'));
        this.echo(movieImage.length + 'Image found :')
        // this.echo(movieImage.join('\n'));
    
        this.echo(outPutJSON());
    });
    
    function outPutJSON() {
        allMovie.detail.push({
            title: movieTitle,
            time: movieTime,
            image: movieImage
        });
        return JSON.stringify(allMovie.detail);
    }
    

    我试过这个:

    function getMovieTime() {
        var time = document.querySelectorAll('ul li ul');
        return Array.prototype.map.call(time, function (e) {
            return e.innerText.substring(0, 5);
        });
    }
    

    1 回复  |  直到 8 年前
        1
  •  1
  •   vatz88    8 年前

    使用了JavaScript函数,如 split , substring join

    var data = [
       {
          "title":[
             "敦克爾克大行動 Dunkirk",
             "女狼嗨到趴 Rough Night",
             "悟空傳 WuKong",
             "猩球崛起 終極決戰 WAR FOR THE PLANET OF THE APES",
             "銀魂 Gintama",
             "蜘蛛人:返校日 Spider-Man: Homecoming",
             "神偷奶爸 3《中文版》  Despicable Me 3 《 CH 》 ",
             "神偷奶爸 3《英文版》  Despicable Me 3《 EN 》"
          ],
          "time":[
             "09:5011:5513:0014:0015:0016:0518:1019:2020:1522:2000:30\n",
             "10:0011:5013:4015:3017:2020:5022:4000:30\n",
             "10:0013:5516:1020:0522:2000:35\n",
             "10:0012:3513:3515:1016:1017:4518:4520:2021:2022:5500:00\n",
             "10:0017:0023:45\n",
             "10:0012:2514:5017:1519:4021:2022:0500:30\n",
             "10:0011:4519:10\n",
             "12:1518:25\n"
          ],
          "image":[
             "picture/zip_20170617231635968.jpg",
             "picture/zip_20170528225556605.jpg",
             "picture/zip_20170528231038563.jpg",
             "picture/zip_20170617225530468.jpg",
             "picture/zip_20170617231105860.jpg",
             "picture/zip_20170617224807245.jpg",
             "picture/zip_20170617223724641.jpg",
             "picture/zip_20170617223542725.jpg"
          ]
       }
    ];
    
    for(var i=0; i<data.length; i++){
    	var movie = data[i];
    	movie.time = getProperTime(movie.time);
    }
    
    console.log(movie.time[0]);
    
    function getProperTime(time){
      for(var i=0;i<time.length; i++){
        time[i] = time[i].split('\n')[0].split(":");
        for(var j=1; j<time[i].length-1; j++){
          time[i][j] = time[i][j].substring(0, 2) + "," + time[i][j].substring(2);
        }
        time[i] = time[i].join(":").split(",");
      }
      return time;
    }