crawl data and save array to json
-
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.
-
Nếu sử dụng array thì hãy dùng array.push(data) bạn à
-
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 "