-
Notifications
You must be signed in to change notification settings - Fork 0
/
amonaz_crawl.go
182 lines (159 loc) · 3.84 KB
/
amonaz_crawl.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
package main
import (
"fmt"
"net/http"
"strings"
"log"
"io/ioutil"
"github.com/PuerkitoBio/goquery"
"encoding/xml"
"os"
"runtime"
// "strconv"
)
type ItemNode struct{
Loc string `xml:"loc"`
Lastmod string `xml:"lastmod"`
}
type Result struct {
XMLName xml.Name `xml:"sitemapindex"`
ItemNode []ItemNode `xml:"sitemap"`
}
type UrlNode struct{
Loc string `xml:"loc"`
Lastmod string `xml:"lastmod"`
Changefreq string `xml:"changefreq"`
Priority string `xml:"priority"`
}
type ItemDetail struct {
XMLName xml.Name `xml:"urlset"`
UrlNodeList [] UrlNode `xml:"url"`
}
type AmazonCrawl struct {
stop chan bool
// popOdd chan string
output chan string
outputstop chan bool
f *os.File
}
const (
outputBufferLen = 256
goroutineCnt = 8
)
func newAmazonCrawl(fileName string) *AmazonCrawl{
ret := new(AmazonCrawl)
ret.output = make(chan string, outputBufferLen)
ret.outputstop = make(chan bool)
ret.f,_ = os.Create(fileName)
return ret
}
func (this *AmazonCrawl)ParseOutput(){
fmt.Println("start output")
for {
select {
case statu := <-this.outputstop:
fmt.Println("outputstop!!!!!!!",statu )
return
default:
for itemInfo:= range this.output{
//fmt.Println("output:",itemInfo)
this.f.WriteString(itemInfo)
}
}
}
fmt.Println("end ParseOutput")
}
func ParseHtmlPage(url string) (ItemInfoPrice string){
//fmt.Println("start paser ", url)
var doc *goquery.Document
var e error
var ret string
if doc, e = goquery.NewDocument(url); e != nil {
panic(e.Error())
}
bNewLine:=false
doc.Find("div.buying .parseasinTitle #btAsinTitle").Each(func(i int, s* goquery.Selection){
ret = s.Text()
bNewLine = true
})
// Find the review items (the type of the Selection would be *goquery.Selection)
doc.Find("#actualPriceValue .priceLarge").Each(func(i int, s *goquery.Selection){
ret = ret + "\t" + s.Text()
})
if bNewLine {
ret = ret + "\n"
}
fmt.Println(ret)
return ret
}
func (this *AmazonCrawl)DoAction(nodeList [] UrlNode){
for _,a := range nodeList{
ret := ParseHtmlPage(a.Loc)
this.output <- ret
}
}
func (this *AmazonCrawl)ParseSiteMap(url string){
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
data, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
v := Result{}
err = xml.Unmarshal([]byte(data), &v)
if err != nil {
fmt.Printf("error: %v", err)
return
}
fmt.Printf("XMLName: %#v\n", v.XMLName)
visitePage := 0
for _, node:= range v.ItemNode[1:] {
var res *http.Response
var err error
if (strings.Index(node.Loc,"reviewdetail") == -1){
res, err = http.Get(node.Loc)
if err != nil {
log.Fatal(err)
}
}else{
continue
}
data, err := ioutil.ReadAll(res.Body)
defer res.Body.Close()
if err == nil {
v := ItemDetail{}
err = xml.Unmarshal([]byte(data), &v)
if err != nil {
log.Fatal(err)
}
Len := len(v.UrlNodeList)
for i:=1; i< goroutineCnt;i++{
fmt.Println(Len*(i-1)/goroutineCnt, Len*i/goroutineCnt)
go this.DoAction(v.UrlNodeList[ Len * (i-1) /goroutineCnt: Len * i /goroutineCnt])
}
}else{
fmt.Println(node.Loc)
log.Fatal(err)
}
}
this.stop <- true
fmt.Println("visitePage:", visitePage)
}
func (this *AmazonCrawl)Start(url string){
//go this.ParseHtmlPageThread()
go this.ParseOutput()
this.ParseSiteMap(url)
fmt.Println("end start")
return
}
func main(){
runtime.GOMAXPROCS(runtime.NumCPU())
aCrawl := newAmazonCrawl("amazon_item_price.txt")
aCrawl.Start("http://www.amazon.cn/sitemap_feed_index1.xml")
//aCrawl.End()
//ParseSiteMap("http://www.amazon.cn/sitemap_feed_index1.xml", writeF)
//ParseHtmlPage("http://www.amazon.cn/Die-Berufung-Des-Deutschen-Ordens-Gegen-Die-Preussen-Rethwisch-Conrad/dp/1168854989")
}