/
index.go
69 lines (48 loc) · 930 Bytes
/
index.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
package main
import (
"github.com/PuerkitoBio/goquery"
"fmt"
"regexp"
"time"
"strconv"
)
var digitsRegexp = regexp.MustCompile(`dp/(.*)\/ref`)
var chanM chan int
func main() {
t1 := time.Now()
chanM = make(chan int, 100)
for i:=1; i < 401; i++ {
go getAllData("http://www.amazon.co.jp/b/?node=2421961051&page=" + strconv.Itoa(i), i)
}
j := 0
LOOP:
for {
select {
case <-chanM:
j++
if j == 400 {
break LOOP
}
}
}
fmt.Println(time.Now().Sub(t1))
}
func getAllData(link string, i int) {
fmt.Println(link)
doc, err := goquery.NewDocument(link)
if err != nil {
fmt.Println(err)
}
j := 0
doc.Find(".s-result-item").Each(func(i int, s *goquery.Selection) {
j++
linkHref, exists := s.Find("a").Attr("href")
if !exists {
fmt.Println("no href")
}
//fmt.Println(linkHref)
all := digitsRegexp.FindStringSubmatch(linkHref)
fmt.Println(all[1])
})
chanM <- i
}