-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
67 lines (55 loc) · 1.59 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import * as fs from 'fs';
import { parse } from 'csv-parse/sync';
import { stringify } from 'csv-stringify/sync';
import { JSDOM } from 'jsdom';
import jQuery from 'jquery';
// DOM設定(HTML読み込み)
const html = fs.readFileSync('sample.html');
const window = new JSDOM(html).window;
const $ = jQuery(window);
// console.log(window.document.querySelector('html').outerHTML);
// 抽出パターン
const searchPattern = {
heading: "main h2",
category: "p",
number: "code",
regexp: /[0-9]/
}
// 出力用配列
// let outputFormat = [
// { heading: "test", category: "カテゴリ", detail: "詳細", no: "No." }
// ];
// CSV入力・変換
const csvData = fs.readFileSync('12CHIBA.CSV');
const csvObj = parse(csvData);
// Table作成
let tableRowTag = "";
tableRowTag += '<table border="1" cellspacing="0" cellpadding="5">\n';
const city = '浦安市';
for (let i = 0; i < csvObj.length; i++) {
const el = csvObj[i];
if (el[7] == city) {
tableRowTag += "<tr>\n";
el.forEach(element => {
tableRowTag += "<td>" + element + "</td>\n";
});
tableRowTag += "</tr>\n";
}
}
tableRowTag += "</table>\n";
// HTML出力
$('body').html(tableRowTag);
fs.writeFileSync('output.html', '<!DOCTYPE html>' + window.document.querySelector('html').outerHTML);
// HTMLから対象レコード抽出
let records = [];
$('tr').each(function() {
let ary = [];
$(this).find('td').each(function() {
let str = $(this).text().trim();
ary.push(str);
});
records.push(ary);
});
// CSV変換・出力
const outputStr = stringify(records);
fs.writeFileSync('output.csv', outputStr);