-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
75 lines (66 loc) · 1.8 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"github.com/paulvollmer/htmltable2csv/scraper"
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
func usage() {
fmt.Println("Usage: htmltable2csv [flags]")
fmt.Println("\nFlags:")
flag.PrintDefaults()
fmt.Println("\nExamples:")
fmt.Println(" htmltable2csv -url 'https://www.w3schools.com/html/html_tables.asp' -selector '#customers > tbody > tr' -csv data.csv")
fmt.Println("\nAuthor : Paul Vollmer")
fmt.Println("Repository : https://github.com/paulvollmer/htmltable2csv")
fmt.Println("")
}
func main() {
flagVersion := flag.Bool("v", false, "Print the version and exit")
flagSource := flag.String("source", "", "The filepath or website url")
flagSelector := flag.String("selector", "", "The table css selector")
flagStart := flag.Int("start", 0, "The row to start collecting data")
flagStop := flag.Int("stop", 0, "The row to stop collecting data")
flagTrim := flag.Bool("trim", false, "Trim the whitespace for each table column")
flagCSV := flag.String("csv", "", "The csv filename. if empty, print csv to stdout")
flag.Usage = usage
flag.Parse()
if *flagVersion {
fmt.Printf("%v, commit %v, built at %v", version, commit, date)
os.Exit(0)
}
if *flagSource == "" {
fmt.Println("Flag -source cannot be empty")
os.Exit(1)
}
if *flagSelector == "" {
fmt.Println("Flag -selector cannot be empty")
os.Exit(1)
}
var err error
scraper := htmltable2csv.Scraper{}
scraper.Source = *flagSource
scraper.Selector = *flagSelector
scraper.Trim = *flagTrim
scraper.Start = *flagStart
scraper.Stop = *flagStop
_, err = scraper.Scrape()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if *flagCSV == "" {
err = scraper.CSV(os.Stdout)
} else {
err = scraper.WriteCSV(*flagCSV)
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}