-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.go
136 lines (117 loc) · 3.7 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
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
// iterscraper scrapes information from a website where URLs contain an incrementing integer.
// Information is retrieved from HTML5 elements, and outputted as a CSV.
package main
import (
"encoding/csv"
"flag"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"sync"
"github.com/PuerkitoBio/goquery"
)
func main() {
var (
urlTemplate = flag.String("url", "http://example.com/v/%d", "The URL you wish to scrape, containing \"%d\" where the id should be substituted")
idLow = flag.Int("from", 0, "The first ID that should be searched in the URL - inclusive.")
idHigh = flag.Int("to", 1, "The last ID that should be searched in the URL - exclusive")
concurrency = flag.Int("concurrency", 1, "How many scrapers to run in parallel. (More scrapers are faster, but more prone to rate limiting or bandwith issues)")
outfile = flag.String("output", "output.csv", "Filename to export the CSV results")
name = flag.String("nameQuery", ".name", "JQuery-style query for the name element")
address = flag.String("addressQuery", ".address", "JQuery-style query for the address element")
phone = flag.String("phoneQuery", ".phone", "JQuery-style query for the phone element")
email = flag.String("emailQuery", ".email", "JQuery-style query for the email element")
)
flag.Parse()
columns := []string{*name, *address, *phone, *email}
headers := []string{"name", "address", "phone", "email"}
// url and id are added as the first two columns.
headers = append([]string{"url", "id"}, headers...)
// create all tasks and send them to the channel.
type task struct {
url string
id int
}
tasks := make(chan task)
go func() {
for i := *idLow; i < *idHigh; i++ {
tasks <- task{url: fmt.Sprintf(*urlTemplate, i), id: i}
}
close(tasks)
}()
// create workers and schedule closing results when all work is done.
results := make(chan []string)
var wg sync.WaitGroup
wg.Add(*concurrency)
go func() {
wg.Wait()
close(results)
}()
for i := 0; i < *concurrency; i++ {
go func() {
defer wg.Done()
for t := range tasks {
r, err := fetch(t.url, t.id, columns)
if err != nil {
log.Printf("could not fetch %v: %v", t.url, err)
continue
}
results <- r
}
}()
}
if err := dumpCSV(*outfile, headers, results); err != nil {
log.Printf("could not write to %s: %v", *outfile, err)
}
}
func fetch(url string, id int, queries []string) ([]string, error) {
res, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("could not get %s: %v", url, err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
if res.StatusCode == http.StatusTooManyRequests {
return nil, fmt.Errorf("you are being rate limited")
}
return nil, fmt.Errorf("bad response from server: %s", res.Status)
}
// parse body with goquery.
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, fmt.Errorf("could not parse page: %v", err)
}
// extract info we want.
r := []string{url, strconv.Itoa(id)}
for _, q := range queries {
r = append(r, strings.TrimSpace(doc.Find(q).Text()))
}
return r, nil
}
func dumpCSV(path string, headers []string, records <-chan []string) error {
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("unable to create file %s: %v", path, err)
}
defer f.Close()
w := csv.NewWriter(f)
// write headers to file.
if err := w.Write(headers); err != nil {
log.Fatalf("error writing record to csv: %v", err)
}
// write all records.
for r := range records {
if err := w.Write(r); err != nil {
log.Fatalf("could not write record to csv: %v", err)
}
}
w.Flush()
// check for extra errors.
if err := w.Error(); err != nil {
return fmt.Errorf("writer failed: %v", err)
}
return nil
}