Vietnam

    Nodejs.vn

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Tags
    • Groups
    • Search

    crawl data and save array to json

    Hỏi Đáp
    0
    3
    1003
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      duyleomessi last edited by

      Em đang crawl data từ trang web:

      var cheerio = require('cheerio');
      var request = require('request');
      var fs = require('fs');
      request('http://www.onemanga.com/manga/one-piece', function (err, res, body) {
          if(err) {
              console.log('err', err);
          }
      * list item
          console.log('statusCode', res.statusCode);
          var $ = cheerio.load(body);
          $('dl.dl-horizontal').each(function(index) {
              console.log('index: ', index);
              var data = $(this).find('dd:nth-of-type(3) > a').map(function() {
                  return $(this).text();
              }).get().join('');
              console.log('data', data);
              var author= JSON.stringify(data);
           
              fs.appendFileSync('author.json', 
                  "{\n" + ' "author": ' + ' " ' + author + ' " ' + '}'
              );
          });
      })
      

      Khi lưu author vào json thì dữ liệu có dạng { "author": "[ "Oda", "Eiichiro" ]" } nhưng định dạng này bị lỗi do phải dùng ngoặc đơn trong array nên các bác cho em hỏi làm sao để dữ liệu có dạng { "author": "[ 'Oda', 'Eiichiro' ]" }. Em cảm ơn.

      1 Reply Last reply Reply Quote 0
      • T
        Trầnn Anh last edited by

        Nếu sử dụng array thì hãy dùng array.push(data) bạn à 😃

        YoloTEAM

        1 Reply Last reply Reply Quote 0
        • Truong Lee159
          Truong Lee159 last edited by

          bạn tham khảo thử xem : http://stackoverflow.com/questions/5293555/how-do-you-represent-a-json-array-of-strings. Hình như lúc save vào json đâu cần dấu "

          1 Reply Last reply Reply Quote 0
          • First post
            Last post