Battingchart300 commited on
Commit
743aa53
·
verified ·
1 Parent(s): f9f0009

Add HTML Data Extractor

Browse files
Files changed (3) hide show
  1. LICENSE +21 -0
  2. README.md +22 -5
  3. index.html +193 -18
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Battingchart300
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md CHANGED
@@ -1,12 +1,29 @@
1
  ---
2
- title: Html Extractor
3
- emoji: 💻
4
- colorFrom: indigo
5
  colorTo: indigo
6
  sdk: static
7
  pinned: false
8
  license: mit
9
- short_description: HTML Data Extractor
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: HTML Data Extractor
3
+ emoji: 🔎
4
+ colorFrom: blue
5
  colorTo: indigo
6
  sdk: static
7
  pinned: false
8
  license: mit
9
+ short_description: Paste HTML and get its tables or links back as CSV or JSON.
10
  ---
11
 
12
+ # HTML Data Extractor
13
+
14
+ Paste HTML into the box and get its data back as a table — **CSV or JSON**, ready to copy or download.
15
+
16
+ ## What it extracts
17
+
18
+ | Mode | Input | Output columns |
19
+ | --- | --- | --- |
20
+ | Tables | any `<table>` in your HTML | the table's own header row, then one row per `<tr>` |
21
+ | Links | any `<a href>` in your HTML | `text`, `href` |
22
+
23
+ If the HTML has more than one table, pick which one to export. A sample table is built in — press **Load sample** to try it.
24
+
25
+ No API key, no server, no fetching — everything runs in your browser, on HTML you paste yourself. It never downloads a page from a URL: paste only HTML you are allowed to use (your own pages, or practice pages such as quotes.toscrape.com).
26
+
27
+ ## License
28
+
29
+ MIT — see `LICENSE`.
index.html CHANGED
@@ -1,19 +1,194 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </html>
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>HTML Data Extractor</title>
7
+ <style>
8
+ :root { --p: #00f0ff; --bg: #07070c; --panel: #12121c; --line: rgba(255,255,255,.12); }
9
+ * { box-sizing: border-box; margin: 0; padding: 0; }
10
+ body { background: var(--bg); color: #eee; font-family: 'Segoe UI', Arial, sans-serif; padding: 24px; }
11
+ .wrap { max-width: 860px; margin: 0 auto; }
12
+ h1 { font-size: 26px; margin-bottom: 6px; }
13
+ h1 span { color: var(--p); }
14
+ p.sub { color: #9aa; margin-bottom: 20px; }
15
+ .card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px; margin-bottom: 16px; }
16
+ label { display: block; font-size: 13px; color: #aaa; margin: 12px 0 4px; }
17
+ label:first-child { margin-top: 0; }
18
+ select, textarea { width: 100%; padding: 10px; border-radius: 8px; border: 1px solid var(--line); background: #0a0a12; color: #fff; font-size: 14px; }
19
+ textarea { min-height: 160px; font-family: Consolas, monospace; font-size: 13px; resize: vertical; }
20
+ .row { display: flex; gap: 12px; }
21
+ .row > div { flex: 1; }
22
+ pre { background: #0a0a12; border: 1px solid var(--line); border-radius: 10px; padding: 14px; overflow-x: auto; font-size: 13px; line-height: 1.5; white-space: pre; margin-top: 8px; }
23
+ h2 { font-size: 16px; margin-bottom: 4px; }
24
+ .btns { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
25
+ button { padding: 9px 16px; border-radius: 8px; border: 1px solid var(--line); background: #1c1c2a; color: #fff; cursor: pointer; font-size: 14px; }
26
+ button.primary { background: var(--p); color: #000; border-color: var(--p); font-weight: 700; }
27
+ button:hover { opacity: .85; }
28
+ .note { color: #888; font-size: 13px; margin-top: 14px; }
29
+ .status { color: #9aa; font-size: 13px; margin-top: 8px; }
30
+ table.preview { width: 100%; border-collapse: collapse; margin-top: 10px; font-size: 13px; }
31
+ table.preview th, table.preview td { border: 1px solid var(--line); padding: 6px 8px; text-align: left; }
32
+ table.preview th { background: #1c1c2a; }
33
+ </style>
34
+ </head>
35
+ <body>
36
+ <div class="wrap">
37
+ <h1>HTML Data <span>Extractor</span></h1>
38
+ <p class="sub">Paste HTML, get a table back as CSV or JSON. No fetching, no server — everything runs in your browser, on HTML you paste yourself.</p>
39
+
40
+ <div class="card">
41
+ <div class="row">
42
+ <div>
43
+ <label for="mode">Extract</label>
44
+ <select id="mode">
45
+ <option value="tables">Tables (&lt;table&gt;)</option>
46
+ <option value="links">Links (&lt;a href&gt;)</option>
47
+ </select>
48
+ </div>
49
+ <div>
50
+ <label for="tablePick">Table</label>
51
+ <select id="tablePick"></select>
52
+ </div>
53
+ </div>
54
+ <label for="input">Your HTML</label>
55
+ <textarea id="input" placeholder="&lt;table&gt;&lt;tr&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Price&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Widget&lt;/td&gt;&lt;td&gt;9.99&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;"></textarea>
56
+ <div class="btns">
57
+ <button class="primary" onclick="extract()">Extract</button>
58
+ <button onclick="loadSample()">Load sample</button>
59
+ <button onclick="clearAll()">Clear</button>
60
+ </div>
61
+ <p class="status" id="status"></p>
62
+ </div>
63
+
64
+ <div class="card" id="result-card" style="display:none">
65
+ <h2>Preview</h2>
66
+ <div id="preview"></div>
67
+ <h2 style="margin-top:16px">CSV</h2>
68
+ <pre id="out-csv"></pre>
69
+ <h2 style="margin-top:16px">JSON</h2>
70
+ <pre id="out-json"></pre>
71
+ <div class="btns">
72
+ <button class="primary" onclick="copyText('out-csv')">Copy CSV</button>
73
+ <button onclick="downloadText('out-csv', 'extracted.csv')">Download CSV</button>
74
+ <button onclick="copyText('out-json')">Copy JSON</button>
75
+ <button onclick="downloadText('out-json', 'extracted.json')">Download JSON</button>
76
+ </div>
77
+ </div>
78
+
79
+ <p class="note">Tip: paste only HTML you are allowed to use — your own pages, or pages made for practice such as quotes.toscrape.com. This tool never fetches a URL by itself.</p>
80
+ </div>
81
+ <script>
82
+ function escapeCsv(v) {
83
+ v = String(v == null ? '' : v);
84
+ return /[",\n]/.test(v) ? '"' + v.replace(/"/g, '""') + '"' : v;
85
+ }
86
+ function rowsToCSV(rows) {
87
+ return rows.map(function (r) { return r.map(escapeCsv).join(','); }).join('\n');
88
+ }
89
+ function rowsToJSON(rows) {
90
+ if (!rows.length) return '[]';
91
+ var head = rows[0];
92
+ return JSON.stringify(rows.slice(1).map(function (r) {
93
+ var o = {};
94
+ head.forEach(function (h, i) { o[h || ('col' + (i + 1))] = r[i] == null ? '' : r[i]; });
95
+ return o;
96
+ }), null, 2);
97
+ }
98
+ function tableRows(table) {
99
+ var out = [];
100
+ var trs = table.querySelectorAll('tr');
101
+ trs.forEach(function (tr) {
102
+ var cells = tr.querySelectorAll('th, td');
103
+ if (cells.length) out.push(Array.prototype.map.call(cells, function (c) { return c.textContent.trim(); }));
104
+ });
105
+ return out;
106
+ }
107
+ function loadSample() {
108
+ document.getElementById('input').value =
109
+ '<table>\n <tr><th>Name</th><th>Price</th><th>In stock</th></tr>\n' +
110
+ ' <tr><td>Widget A</td><td>9.99</td><td>yes</td></tr>\n' +
111
+ ' <tr><td>Widget B</td><td>14.50</td><td>no</td></tr>\n</table>';
112
+ extract();
113
+ }
114
+ function clearAll() {
115
+ document.getElementById('input').value = '';
116
+ document.getElementById('result-card').style.display = 'none';
117
+ document.getElementById('status').textContent = '';
118
+ }
119
+ function showRows(rows, label) {
120
+ var card = document.getElementById('result-card');
121
+ if (!rows.length) {
122
+ card.style.display = 'none';
123
+ document.getElementById('status').textContent = label;
124
+ return;
125
+ }
126
+ card.style.display = 'block';
127
+ document.getElementById('status').textContent = rows.length + ' rows found.';
128
+ var html = '<table class="preview">';
129
+ rows.forEach(function (r, i) {
130
+ html += '<tr>' + r.map(function (c) {
131
+ var tag = i === 0 ? 'th' : 'td';
132
+ return '<' + tag + '>' + String(c).replace(/&/g, '&amp;').replace(/</g, '&lt;') + '</' + tag + '>';
133
+ }).join('') + '</tr>';
134
+ });
135
+ document.getElementById('preview').innerHTML = html + '</table>';
136
+ document.getElementById('out-csv').textContent = rowsToCSV(rows);
137
+ document.getElementById('out-json').textContent = rowsToJSON(rows);
138
+ }
139
+ var lastDoc = null;
140
+ function extract() {
141
+ var html = document.getElementById('input').value;
142
+ var mode = document.getElementById('mode').value;
143
+ var pick = document.getElementById('tablePick');
144
+ if (!html.trim()) {
145
+ document.getElementById('status').textContent = 'Paste some HTML first — or press "Load sample".';
146
+ return;
147
+ }
148
+ lastDoc = new DOMParser().parseFromString(html, 'text/html');
149
+ if (mode === 'links') {
150
+ var links = Array.prototype.map.call(lastDoc.querySelectorAll('a[href]'), function (a) {
151
+ return [a.textContent.trim(), a.getAttribute('href')];
152
+ });
153
+ showRows([['text', 'href']].concat(links), 'No links found in this HTML.');
154
+ pick.innerHTML = '';
155
+ return;
156
+ }
157
+ var tables = lastDoc.querySelectorAll('table');
158
+ if (!tables.length) {
159
+ showRows([], 'No <table> found in this HTML. Try "Links" mode, or paste HTML that contains a table.');
160
+ pick.innerHTML = '';
161
+ return;
162
+ }
163
+ pick.innerHTML = '';
164
+ tables.forEach(function (t, i) {
165
+ var opt = document.createElement('option');
166
+ opt.value = i;
167
+ opt.textContent = 'Table ' + (i + 1) + ' — ' + t.querySelectorAll('tr').length + ' rows';
168
+ pick.appendChild(opt);
169
+ });
170
+ showRows(tableRows(tables[Number(pick.value) || 0]), '');
171
+ }
172
+ function copyText(id) {
173
+ var t = document.getElementById(id).textContent;
174
+ if (navigator.clipboard) navigator.clipboard.writeText(t);
175
+ }
176
+ function downloadText(id, name) {
177
+ var t = document.getElementById(id).textContent;
178
+ var a = document.createElement('a');
179
+ a.href = URL.createObjectURL(new Blob([t], { type: 'text/plain' }));
180
+ a.download = name; a.click();
181
+ }
182
+ document.getElementById('mode').addEventListener('change', function () {
183
+ document.getElementById('tablePick').disabled = this.value === 'links';
184
+ if (document.getElementById('input').value.trim()) extract();
185
+ });
186
+ document.getElementById('tablePick').addEventListener('change', function () {
187
+ if (!lastDoc) return;
188
+ var tables = lastDoc.querySelectorAll('table');
189
+ showRows(tableRows(tables[Number(this.value) || 0]), '');
190
+ });
191
+ if (typeof module !== 'undefined') { module.exports = { escapeCsv: escapeCsv, rowsToCSV: rowsToCSV, rowsToJSON: rowsToJSON }; }
192
+ </script>
193
+ </body>
194
  </html>