-
Notifications
You must be signed in to change notification settings - Fork 8
/
page.go
398 lines (368 loc) · 8.64 KB
/
page.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// htmltable enables structured data extraction from HTML tables and URLs
package htmltable
import (
"context"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"golang.org/x/net/html"
)
// mock for tests
var htmlParse = html.Parse
// Page is the container for all tables parseable
type Page struct {
Tables []*Table
ctx context.Context
rowSpans []int
colSpans []int
row []string
rows [][]string
maxCols int
// current row
colSpan []int
rowSpan []int
// all
cSpans [][]int
rSpans [][]int
}
// New returns an instance of the page with possibly more than one table
func New(ctx context.Context, r io.Reader) (*Page, error) {
p := &Page{ctx: ctx}
return p, p.init(r)
}
// NewFromString is same as New(ctx.Context, io.Reader), but from string
func NewFromString(r string) (*Page, error) {
return New(context.Background(), strings.NewReader(r))
}
// NewFromResponse is same as New(ctx.Context, io.Reader), but from http.Response.
//
// In case of failure, returns `ResponseError`, that could be further inspected.
func NewFromResponse(resp *http.Response) (*Page, error) {
p, err := New(resp.Request.Context(), resp.Body)
if err != nil {
return nil, err
}
return p, nil
}
// NewFromURL is same as New(ctx.Context, io.Reader), but from URL.
//
// In case of failure, returns `ResponseError`, that could be further inspected.
func NewFromURL(url string) (*Page, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
if resp.Body != nil {
defer resp.Body.Close()
}
return NewFromResponse(resp)
}
// Len returns number of tables found on the page
func (p *Page) Len() int {
return len(p.Tables)
}
// FindWithColumns performs fuzzy matching of tables by given header column names
func (p *Page) FindWithColumns(columns ...string) (*Table, error) {
// realistic p won't have this much
found := 0xfffffff
for idx, table := range p.Tables {
matchedColumns := 0
for _, col := range columns {
for _, header := range table.Header {
if col == header {
// perform fuzzy matching of table headers
matchedColumns++
}
}
}
if matchedColumns != len(columns) {
continue
}
if found < len(p.Tables) {
// and do a best-effort error message, that is cleaner than pandas.read_html
return nil, fmt.Errorf("more than one table matches columns `%s`: "+
"[%d] %s and [%d] %s", strings.Join(columns, ", "),
found, p.Tables[found], idx, p.Tables[idx])
}
found = idx
}
if found > len(p.Tables) {
return nil, fmt.Errorf("cannot find table with columns: %s",
strings.Join(columns, ", "))
}
return p.Tables[found], nil
}
// Each row would call func with the value of the table cell from the column
// specified in the first argument.
//
// Returns an error if table has no matching column name.
func (p *Page) Each(a string, f func(a string) error) error {
table, err := p.FindWithColumns(a)
if err != nil {
return err
}
offsets := map[string]int{}
for idx, header := range table.Header {
offsets[header] = idx
}
for idx, row := range table.Rows {
if len(row) < 1 {
continue
}
err = f(row[offsets[a]])
if err != nil {
return fmt.Errorf("row %d: %w", idx, err)
}
}
return nil
}
// Each2 will get two columns specified in the first two arguments
// and call the func with those values for every row in the table.
//
// Returns an error if table has no matching column names.
func (p *Page) Each2(a, b string, f func(a, b string) error) error {
table, err := p.FindWithColumns(a, b)
if err != nil {
return err
}
offsets := map[string]int{}
for idx, header := range table.Header {
offsets[header] = idx
}
_1, _2 := offsets[a], offsets[b]
for idx, row := range table.Rows {
if len(row) < 2 {
continue
}
err = f(row[_1], row[_2])
if err != nil {
return fmt.Errorf("row %d: %w", idx, err)
}
}
return nil
}
// Each3 will get three columns specified in the first three arguments
// and call the func with those values for every row in the table.
//
// Returns an error if table has no matching column names.
func (p *Page) Each3(a, b, c string, f func(a, b, c string) error) error {
table, err := p.FindWithColumns(a, b, c)
if err != nil {
return err
}
offsets := map[string]int{}
for idx, header := range table.Header {
offsets[header] = idx
}
_1, _2, _3 := offsets[a], offsets[b], offsets[c]
for idx, row := range table.Rows {
if len(row) < 3 {
continue
}
err = f(row[_1], row[_2], row[_3])
if err != nil {
return fmt.Errorf("row %d: %w", idx, err)
}
}
return nil
}
func (p *Page) init(r io.Reader) error {
root, err := htmlParse(r)
if err != nil {
return err
}
p.parse(root)
p.finishTable()
return nil
}
func (p *Page) parse(n *html.Node) {
if n == nil {
return
}
switch n.Data {
case "td", "th":
p.colSpan = append(p.colSpan, p.intAttrOr(n, "colspan", 1))
p.rowSpan = append(p.rowSpan, p.intAttrOr(n, "rowspan", 1))
var sb strings.Builder
p.innerText(n, &sb)
p.row = append(p.row, sb.String())
return
case "tr":
p.finishRow()
case "table":
p.finishTable()
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
p.parse(c)
}
}
func (p *Page) intAttrOr(n *html.Node, attr string, default_ int) int {
for _, a := range n.Attr {
if a.Key != attr {
continue
}
val, err := strconv.Atoi(a.Val)
if err != nil {
return default_
}
return val
}
return default_
}
func (p *Page) finishRow() {
if len(p.row) == 0 {
return
}
if len(p.row) > p.maxCols {
p.maxCols = len(p.row)
}
p.rows = append(p.rows, p.row)
p.cSpans = append(p.cSpans, p.colSpan)
p.rSpans = append(p.rSpans, p.rowSpan)
p.row = []string{}
p.colSpan = []int{}
p.rowSpan = []int{}
}
type cellSpan struct {
BeginX, EndX int
BeginY, EndY int
Value string
}
func (d *cellSpan) Match(x, y int) bool {
if d.BeginX > x {
return false
}
if d.EndX <= x {
return false
}
if d.BeginY > y {
return false
}
if d.EndY <= y {
return false
}
return true
}
type spans []cellSpan
func (s spans) Value(x, y int) (string, bool) {
for _, v := range s {
if !v.Match(x, y) {
continue
}
return v.Value, true
}
return "", false
}
func (p *Page) finishTable() {
defer func() {
if r := recover(); r != nil {
firstRow := []string{}
if len(p.rows) > 0 {
firstRow = p.rows[0][:]
}
Logger(p.ctx, "unparsable table", "panic", fmt.Sprintf("%v", r), "firstRow", firstRow)
}
p.rows = [][]string{}
p.colSpans = []int{}
p.rowSpans = []int{}
p.cSpans = [][]int{}
p.rSpans = [][]int{}
p.maxCols = 0
}()
p.finishRow()
if len(p.rows) == 0 {
return
}
rows := [][]string{}
allSpans := spans{}
rowSkips := 0
gotHeader := false
ROWS:
for y := 0; y < len(p.rows); y++ { // rows cols addressable by x
currentRow := []string{}
skipRow := false
k := 0 // next row columns
j := 0 // p.rows cols addressable by j
for x := 0; x < p.maxCols; x++ {
value, ok := allSpans.Value(x, y)
if ok {
currentRow = append(currentRow, value)
continue
}
if gotHeader && len(p.rows[y]) == 1 && p.rows[y][0] == "" {
// this are most likely empty rows or table dividers
rowSkips++
continue ROWS
}
if len(p.rSpans[y]) == j {
break
}
rowSpan := p.rSpans[y][j]
colSpan := p.cSpans[y][j]
value = p.rows[y][j]
if gotHeader && (rowSpan > 1 || colSpan > 1) {
allSpans = append(allSpans, cellSpan{
BeginX: x,
EndX: x + colSpan,
BeginY: y,
EndY: y + rowSpan,
Value: value,
})
}
if !gotHeader && colSpan > 1 {
skipRow = true
// in header: merge, in row - duplicate
for q := 0; q < colSpan; q++ {
nextValue := fmt.Sprintf("%s %s", value, p.rows[y+1][k])
currentRow = append(currentRow, nextValue)
k++
}
} else {
currentRow = append(currentRow, value)
}
j++
}
if skipRow {
rowSkips++
y++
}
gotHeader = true
if len(currentRow) > p.maxCols {
p.maxCols = len(currentRow)
}
rows = append(rows, currentRow)
}
header := rows[0]
rows = rows[1:]
Logger(p.ctx, "found table", "columns", header, "count", len(rows))
p.Tables = append(p.Tables, &Table{
Header: header,
Rows: rows,
})
}
func (p *Page) innerText(n *html.Node, sb *strings.Builder) {
if n.Type == html.TextNode {
sb.WriteString(strings.TrimSpace(n.Data))
return
}
if n.FirstChild == nil {
return
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
p.innerText(c, sb)
}
}
// Table is the low-level representation of raw header and rows.
//
// Every cell string value is truncated of its whitespace.
type Table struct {
// Header holds names of headers
Header []string
// Rows holds slice of string slices
Rows [][]string
}
func (table *Table) String() string {
return fmt.Sprintf("Table[%s] (%d rows)", strings.Join(table.Header, ", "), len(table.Rows))
}