Node.js snippet for quick json creation from scraped webpage

Just a quick snippet to build up a json file of all the HSK 3 grammar points in Chinese Grammar Wiki HSK 3 grammar points - Chinese Grammar Wiki for a later use in a iOS Widget:

Install dependencies - modules cheerio and got first.

const cheerio = require("cheerio");
const got = require("got");
const fs = require("fs");

const hsk3URL =
  "https://resources.allsetlearning.com/chinese/grammar/HSK_3_grammar_points";

(async () => {
  const response = await got(hsk3URL);
  const $ = cheerio.load(response.body);
  let result = [];
  let titles = { 0: "title", 1: "description", 2: "example" };

  $("tr").map((i, el) => {
    let item = {};
    $(el)
      .find("td")
      .map((j, el) => {
        let text =
          j == 2 ? $(el).text().trim().replace(/\s/g, "") : $(el).text().trim();
        item[titles[j]] = text;
        let link = $(el).find("a").attr("href");
        if (link) {
          item["link"] = `https://resources.allsetlearning.com${link}`;
        }
      });
    if (item && item.title) {
      result.push(item);
    }
  });
  let data = JSON.stringify(result);
  fs.writeFileSync("hsk3.json", data);
})();



Mastodon