简体   繁体   English

根据提取的数据创建有效的JSON文件

[英]Creating a valid JSON file from pulled data

In a bit of downtime from work I decided to mess around with data scraping and JSON just for a bit of fun. 在下班后的短暂停机中,我决定只使用一些有趣的方法来处理数据抓取和JSON。 So I set up a dummy page on my site which changes a song name and artist every x minutes and created a scraper to pull off the page and write to a JSON file. 因此,我在网站上设置了一个虚拟页面,该页面每隔X分钟更改一次歌曲名称和歌手,并创建了一个刮取器以拉出页面并写入JSON文件。 So far so good. 到现在为止还挺好。 My script is pulling the data from the page when it should and writing it to a JSON file but its coming back invalid. 我的脚本正在按时从页面中提取数据并将其写入JSON文件,但返回时无效。 What I am getting is this 我得到的是这个

{
    "trackName": "Feels So Close",
    "artistTitle": "Calvin Harris",
    "playTime": "9:24"
}{
    "trackName": "",
    "artistTitle": "Studio B",
    "playTime": "9:28"
}

And what I want is more like this 我想要的更像这样

{
    "response1": {
        "trackName": "Feels So Close",
        "artistTitle": "Calvin Harris",
        "playTime": "9:24"
    },
    "response2": {
        "trackName": "",
        "artistTitle": "Studio B",
        "playTime": "9:28"
    }
}

This is the js that pulls the data: 这是提取数据的js:

  request("mySite", function(error, response, body) {
    if(error) {
      console.log("Error: " + error);
    }

    console.log("Status code: " + response.statusCode);

    var $ = cheerio.load(body);

    $('.track').each(function(index){
      var json = { trackName: "", artistTitle: "", playTime: ""};

      var title = $(this).find(".track a").text().trim();
      var artist = $(this).find(".artist").text().trim();
      var hours = new Date().getHours();
      var mins = new Date().getMinutes();

      json.trackName = title;
      json.artistTitle = artist;    
      json.playTime = hours + ":" + mins;

      fs.appendFileSync('trackList.json', JSON.stringify(json, null, 4), function(error){
        if(error) {
          console.log('Error: ' + error);
        };
      });
    });
  });

Any help is greatly appreciated, thanks! 非常感谢任何帮助,谢谢!

You should create an empty object to then use .each() iterate the DOM element and create desired properties ie response1, response2 . 您应该创建一个空对象,然后使用.each()迭代DOM元素并创建所需的属性,即response1, response2

The finally use .appendFileSync() to send data once after iteration. 最终在迭代后使用.appendFileSync()发送一次数据。

//Create empty object
var json = {};   

//Iterate to create complete JSON object
$('.track').each(function(i){
    var title = $(this).find(".track a").text().trim();
    var artist = $(this).find(".artist").text().trim();
    var hours = new Date().getHours();
    var mins = new Date().getMinutes();

    //Create property object
    json['response' + i] = {
        trackName : title;
        artistTitle : artist;    
        playTime : hours + ":" + mins;
    };
});

//Then send it to update
fs.appendFileSync('trackList.json', JSON.stringify(json, null, 4), function(error){
    if(error) {
      console.log('Error: ' + error);
    };
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM