showing results for - "manually parsing html in node js"
Isabella
24 Jun 2017
1/*
2    This code comes from Vincent Lab
3    And it has a video version linked here: https://www.youtube.com/watch?v=PozG7Sva270
4*/
5
6// Import dependencies
7const fs = require("fs");
8const cheerio = require("cheerio");
9const cheerioTableparser = require("cheerio-tableparser");
10
11// Get all the filenames from the customers folder
12const files = fs.readdirSync("customers");
13
14// All of the parse customers
15let customers = [];
16
17// For each file in the customers folder
18for (const file of files) {
19
20    // The parse customer
21    let customer = {};
22
23    // Get the HTML out of the file
24    const html = fs.readFileSync(`customers/${file}`).toString();
25
26    // Convert the HTML to a cheerio dom element
27    const $ = cheerio.load(html);
28
29    // Run HTML through table parser
30    cheerioTableparser($);
31
32    // Parse the table and turn it into an array
33    let table = $("table").parsetable();
34
35    // Check if it's format one or format two
36    if (table[1][2].match(/\d+-\d+-\d+/) !== null) {
37
38        // Add the data from the table to the customer object
39        customer = {
40            name: table[1][0],
41            telephone: [table[1][1], table[1][2]],
42            birthday: table[1][3],
43            emailAddress: table[1][4],
44            employment: table[1][5],
45            vehicle: table[1][6],
46            bank: table[1][7],
47        }
48
49    } else {
50
51        // Add the data from the table to the customer object
52        customer = {
53            name: table[1][0],
54            telephone: table[1][1],
55            birthday: table[1][2],
56            emailAddress: table[1][3],
57            employment: table[1][4],
58            vehicle: table[1][5],
59            bank: table[1][6],
60        }
61
62    }
63
64    // Add the customer to the customers array
65    customers.push(customer);
66}
67
68// Save the extracted information to a json file
69fs.writeFileSync("customers.json", JSON.stringify(customers));