Esempio n. 1
0
// 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 *robot.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())

}
Esempio n. 2
0
//主页解析
func (self *Www79xsComProcessor) mainParse(p *robot.Page) {
	//开始解析页面
	query := p.GetHtmlParser()
	query.Find(".subnav ul li a").Each(func(i int, s *goquery.Selection) {
		addr, _ := s.Attr("href")
		if addr == utils.GirlUrl {
			p.AddTargetRequest(utils.InitRequest(utils.BaseUrl+addr, map[string]string{"first": utils.GIRL}, self.urlListParse))
		} else {
			p.AddTargetRequest(utils.InitRequest(utils.BaseUrl+addr, map[string]string{"first": utils.BOY}, self.urlListParse))
		}
	})
}
Esempio n. 3
0
//解析小说详情页
func (self *Www79xsComProcessor) introParse(p *robot.Page) {
	meta := p.GetRequest().GetMeta().(map[string]string)
	//开始解析页面
	query := p.GetHtmlParser()
	intro := query.Find("#info h3 p").Eq(1).Text()
	img, _ := query.Find(".img img").Attr("src")
	// 小说章节列表地址
	chaptersource, _ := query.Find(".b1 a").Attr("href")
	tmp := utils.MapCopy(meta)
	tmp["introduction"] = intro
	tmp["img"] = utils.BaseUrl + img
	tmp["chaptersource"] = utils.BaseUrl + chaptersource
	p.AddTargetRequest(utils.InitRequest(utils.BaseUrl+chaptersource, tmp, self.chaperParse))
}
Esempio n. 4
0
//小说章节解析
func (self *Www79xsComProcessor) chaperParse(p *robot.Page) {
	meta := p.GetRequest().GetMeta().(map[string]string)
	//开始解析页面
	query := p.GetHtmlParser()
	query.Find(".insert_list li").Each(func(i int, s *goquery.Selection) {
		tmp := utils.MapCopy(meta)
		tmp["chapter"] = strconv.Itoa(i)
		tmp["subtitle"] = s.Find("strong a").Text()
		addr, _ := s.Find("strong a").Attr("href")
		tmp["contenturl"] = p.GetRequest().GetBaseUrl() + addr
		//检测contenturl, 如果数据库中存在,则跳过本次抓取,如果不存在则将url加入调度队列
		//这个需求有时间再做
		if len(tmp["subtitle"]) != 0 {
			p.AddTargetRequest(utils.InitRequest(tmp["contenturl"], tmp, self.contentParse))
		}
	})
}
Esempio n. 5
0
//分类列表解析
func (self *Www79xsComProcessor) classParse(p *robot.Page) {
	meta := p.GetRequest().GetMeta().(map[string]string)
	//开始解析页面
	query := p.GetHtmlParser()
	query.Find("div .yl_nr_lt2 ul").Each(func(i int, s *goquery.Selection) {
		//获取二级分类, 小说标题,作者
		second := s.Find(".ynl2 a").Text()
		title := s.Find(".ynl3 a").Eq(1).Text()
		author := s.Find(".ynl6 a").Text()
		novelsource := utils.BaseUrl + func() string {
			addr, _ := s.Find(".ynl3 a").Eq(1).Attr("href")
			return addr
		}()
		tmp := make(map[string]string)
		tmp["first"] = meta["first"]
		tmp["second"] = second
		tmp["title"] = title
		tmp["author"] = author
		tmp["novelsource"] = novelsource
		p.AddTargetRequest(utils.InitRequest(novelsource, tmp, self.introParse))
	})
}
Esempio n. 6
0
//获取分类页面的url list,并解析
func (self *Www79xsComProcessor) urlListParse(p *robot.Page) {
	meta := p.GetRequest().GetMeta()
	//开始解析页面
	query := p.GetHtmlParser()
	//获取尾页addr
	lastaddr, ok := query.Find("tbody a").Last().Attr("href")
	if ok {
		//解析addr
		kv := goutils.GetKVInRelaPath(lastaddr)
		//url拼接
		maxpage, _ := strconv.Atoi(kv["page"])
		for i := 1; i <= maxpage; i++ {
			page := strconv.Itoa(i)
			p.AddTargetRequest(utils.InitRequest(
				"http://www.79xs.com/Book/ShowBookList.aspx?tclassid="+kv["tclassid"]+"&page="+page,
				meta.(map[string]string),
				self.classParse))
		}
	} else {
		p.AddTargetRequest(utils.InitRequest(p.GetRequest().GetUrl(), meta.(map[string]string), self.classParse))
	}
}