/
diandianPostSpider.go
executable file
·185 lines (151 loc) · 5.31 KB
/
diandianPostSpider.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
package main
import (
"fmt"
"github.com/hu17889/go_spider/core/common/page"
"github.com/hu17889/go_spider/core/pipeline"
"github.com/hu17889/go_spider/core/spider"
"html"
"os"
"regexp"
"strings"
//"sync"
)
type MyPageProcesser struct {
visit_url map[string]int
newurl chan string
}
func NewMyPageProcesser() *MyPageProcesser {
return &MyPageProcesser{}
}
func (this *MyPageProcesser) Init() {
this.visit_url = make(map[string]int)
//this.newurl = make(chan string, 10)
//fmt.Println("process init")
}
// Parse html dom here and record the parse result that we want to Page.
// Package goquery (http://godoc.org/github.com/PuerkitoBio/goquery) is used to parse html.
func (this *MyPageProcesser) Process(p *page.Page) {
if !p.IsSucc() {
println(p.Errormsg())
return
}
var crawok bool
crawok = false
//query := p.GetHtmlParser()
//var urls []string
//fmt.Println(p.GetBodyStr())
re := regexp.MustCompile(`<a href="(.*?)">(.*?)`)
sectUrlsTemp := re.FindAllSubmatch([]byte(p.GetBodyStr()), -1)
for _, url := range sectUrlsTemp {
for _, url1 := range url {
crawok = true
http_index := strings.Index(string(url1), "http://shinichr.diandian.com")
http_note := strings.Index(string(url1), "\"")
http_quote := strings.Index(string(url1), "#")
if http_index >= 0 && http_quote < 0 {
if http_note > 0 && http_note < http_index {
continue
}
var http_url string
if http_note <= 0 {
http_url = string(url1)[http_index:]
} else {
http_url = string(url1)[http_index:http_note]
}
if this.visit_url[http_url] == 0 {
this.visit_url[http_url] = 1
fmt.Println("####unvisited:", http_url)
//fmt.Println("###AddTargetRequest:", http_url)
p.AddTargetRequest(http_url, "html")
}
}
}
}
if crawok == false {
fmt.Println("crawl false:*****************", p.GetRequest().GetUrl())
http_page := strings.Index(p.GetRequest().GetUrl(), "http://shinichr.diandian.com/page")
http_post := strings.Index(p.GetRequest().GetUrl(), "http://shinichr.diandian.com/post")
fmt.Println("http_page:", http_page, "http_post:", http_post)
if http_page >= 0 || http_post >= 0 {
//this.visit_url[p.GetRequest().GetUrl()] = 0
p.AddTargetRequest(p.GetRequest().GetUrl(), "html")
}
}
http_index := strings.Index(p.GetRequest().GetUrl(), "http://shinichr.diandian.com/post/")
// rex, _ := regexp.Compile("\\/")
//replaceurl := rex.ReplaceAllString(p.GetRequest().GetUrl(), ".")
//fmt.Println("http_index=", http_index)
//fmt.Println("replaceurl=", p.GetRequest().GetUrl()[http_index:], "....", http_index)
if http_index >= 0 {
cuturl := p.GetRequest().GetUrl()[34:]
//fmt.Println("replaceurl=", cuturl)
rex, _ := regexp.Compile("\\/")
replaceurl := rex.ReplaceAllString(cuturl, ".")
filedir := fmt.Sprintf("/home/shinichr/diandian_post/%s", replaceurl)
fout, err := os.Create(filedir)
if err != nil {
fmt.Println(filedir, err)
return
}
defer fout.Close()
src := p.GetBodyStr()
re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
src = re.ReplaceAllStringFunc(src, strings.ToLower)
//去除STYLE
re, _ = regexp.Compile("\\<style[\\S\\s]+?\\</style\\>")
src = re.ReplaceAllString(src, "")
//去除SCRIPT
re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>")
src = re.ReplaceAllString(src, "")
//去除所有尖括号内的HTML代码,并换成换行符
re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
src = re.ReplaceAllString(src, "\n")
//去除连续的换行符
re, _ = regexp.Compile("\\s{2,}")
src = re.ReplaceAllString(src, "\n")
//fmt.Println(strings.TrimSpace(src))
fout.WriteString(html.UnescapeString(src))
fmt.Println("save file ", filedir)
}
//query.Find(`div[class="rich-content] div[class="post"] div[class="post-top"] div[class="post-content post-text"] a`).Each(func(i int, s *goquery.Selection) {
/*query.Find("div.content").Each(func(i int, s *goquery.Selection) {
href, _ := s.Attr("href")
http_index := strings.Index(href, "http")
http_url := href[http_index:]
//fmt.Println("###url:\n", http_url, "=", this.visit_url[http_url])
//this.newurl <- http_url
if this.visit_url[http_url] == 0 {
this.visit_url[http_url] = 1
fmt.Println("###AddTargetRequest:", http_url)
p.AddTargetRequest(http_url, "html")
}
urls = append(urls, href)
})*/
// these urls will be saved and crawed by other coroutines.
/*doc, _ := goquery.NewDocument("http://shinichr.diandian.com")
doc.Find("a").Each(func(i int, s *goquery.Selection) {
href, _ := s.Attr("href")
fmt.Println("####href=", href)
})*/
//p.AddField("readme", readme)
}
func (this *MyPageProcesser) Finish() {
fmt.Printf("TODO:before end spider \r\n")
}
func main() {
pageProcess := NewMyPageProcesser()
pageProcess.Init()
diandianSpider := spider.NewSpider(pageProcess, "TaskName").
AddUrl("http://shinichr.diandian.com/", "html"). // Start url, html is the responce type ("html" or "json" or "jsonp" or "text")
AddPipeline(pipeline.NewPipelineConsole()). // Print result on screen
AddPipeline(pipeline.NewPipelineFile("/home/shinichr/spider.tmp")).
SetThreadnum(10000) // Crawl request by three Coroutines
for i := 2; i < 10; i++ {
url := fmt.Sprintf("http://shinichr.diandian.com/page/%d", i)
diandianSpider.AddUrl(url, "html")
}
diandianSpider.Run()
for url, _ := range pageProcess.visit_url {
fmt.Println("spider:", url)
}
}