本文整理汇总了Golang中github.com/hu17889/go_spider/core/common/page.Page.AddTargetRequest方法的典型用法代码示例。如果您正苦于以下问题:Golang Page.AddTargetRequest方法的具体用法?Golang Page.AddTargetRequest怎么用?Golang Page.AddTargetRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/hu17889/go_spider/core/common/page.Page
的用法示例。
在下文中一共展示了Page.AddTargetRequest方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Process
// Parse html dom here and record the parse result that we want to crawl.
// Package simplejson (https://github.com/bitly/go-simplejson) is used to parse data of json.
func (this *MyPageProcesser) Process(p *page.Page) {
if !p.IsSucc() {
println(p.Errormsg())
return
}
query := p.GetJson()
status, err := query.GetPath("result", "status", "code").Int()
if status != 0 || err != nil {
log.Panicf("page is crawled error : errorinfo=%s : status=%d : startNewsId=%d", err.Error(), status, this.startNewsId)
}
num, err := query.GetPath("result", "pageStr", "pageSize").Int()
if num == 0 || err != nil {
// Add url of next crawl
startIdstr := strconv.Itoa(this.startNewsId)
p.AddTargetRequest("http://live.sina.com.cn/zt/api/l/get/finance/globalnews1/index.htm?format=json&id="+startIdstr+"&pagesize=10&dire=f", "json")
return
}
var idint, nextid int
var nextidstr string
query = query.Get("result").Get("data")
for i := 0; i < num; i++ {
id, err := query.GetIndex(i).Get("id").String()
if id == "" || err != nil {
continue
}
idint, err = strconv.Atoi(id)
if err != nil {
continue
}
if idint <= this.startNewsId {
break
}
if i == 0 {
nextid = idint
nextidstr = id
}
content, err := query.GetIndex(i).Get("content").String()
if content == "" || err != nil {
continue
}
time, err := query.GetIndex(i).Get("created_at").String()
if err != nil {
continue
}
p.AddField(id+"_id", id)
p.AddField(id+"_content", content)
p.AddField(id+"_time", time)
}
// Add url of next crawl
this.startNewsId = nextid
p.AddTargetRequest("http://live.sina.com.cn/zt/api/l/get/finance/globalnews1/index.htm?format=json&id="+nextidstr+"&pagesize=10&dire=f", "json")
//println(p.GetTargetRequests())
}
示例2: Process
// 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")
//.........这里部分代码省略.........