Przeglądaj źródła

新增翻页信息记录

maxiaoshan 2 lat temu
rodzic
commit
44675e6249
2 zmienionych plików z 69 dodań i 52 usunięć
  1. 43 16
      src/spider/script.go
  2. 26 36
      src/spider/spider.go

+ 43 - 16
src/spider/script.go

@@ -43,20 +43,25 @@ import (
 //脚本
 type Script struct {
 	//Ishttps            bool
-	SCode, ScriptFile   string
-	Encoding            string
-	Userproxy           bool
-	ErrorNum            int32  //错误数
-	Downloader          string //下载器
-	TotalRequestNum     int32  //总请求次数
-	ToDayRequestNum     int32  //今日请求次数
-	YestoDayRequestNum  int32  //昨日请求次数
-	Timeout             int64  //超时时间秒
-	L                   *lua.LState
-	NoDownloadNum       int32           //未成功下载数
-	LastThreeTimes      []time.Duration //单条信息流程完成的时间,最后三次
-	FileLastThreeTimes  []time.Duration //附件下载单条信息流程完成的时间,最后三次
-	AlreadyGetPageHeart map[int]bool    //记录某页已完成心跳记录
+	SCode, ScriptFile  string
+	Encoding           string
+	Userproxy          bool
+	ErrorNum           int32  //错误数
+	Downloader         string //下载器
+	TotalRequestNum    int32  //总请求次数
+	ToDayRequestNum    int32  //今日请求次数
+	YestoDayRequestNum int32  //昨日请求次数
+	Timeout            int64  //超时时间秒
+	L                  *lua.LState
+	NoDownloadNum      int32           //未成功下载数
+	LastThreeTimes     []time.Duration //单条信息流程完成的时间,最后三次
+	FileLastThreeTimes []time.Duration //附件下载单条信息流程完成的时间,最后三次
+	//
+	AlreadyGetPageHeart map[int]bool //记录某页已完成心跳记录
+	MaxPage             int          //采集最大页
+	PageOneTextHash     string       //爬虫第一页页面内容hash
+	PageTwoTextHash     string       //爬虫第二页页面内容hash
+	RecordedHeartInfo   bool         //是否已记录心跳信息(避免记录频率过快)
 }
 
 const (
@@ -500,11 +505,33 @@ func (s *Script) LoadScript(site, channel, user *string, code, script_file strin
 	}))
 	//推送列表页下载数据量
 	s.L.SetGlobal("sendListNum", s.L.NewFunction(func(S *lua.LState) int {
-		//index := S.ToInt(-2)
+		pageno := S.ToInt(-2)
 		//if index == 1 {
 		table := S.ToTable(-1)
 		if table.Len() > 0 {
-			UpdateHeart(*site, *channel, code, *user, "findlist") //记录列表页实际采集数据量心跳
+			//爬虫翻页
+			if s.MaxPage > 1 {
+				if pageno == 1 && s.PageOneTextHash == "" { //记录第一页数据的hash
+					textMap := util.TableToMap(table)
+					textByte, err := json.Marshal(textMap)
+					text := string(textByte)
+					if err == nil && text != "" {
+						s.PageOneTextHash = util.HexText(text)
+					}
+				} else if pageno == 2 && s.PageTwoTextHash == "" {
+					textMap := util.TableToMap(table)
+					textByte, err := json.Marshal(textMap)
+					text := string(textByte)
+					if err == nil && text != "" {
+						s.PageTwoTextHash = util.HexText(text)
+					}
+				}
+			}
+			//爬虫心跳
+			if !s.RecordedHeartInfo {
+				UpdateHeart(*site, *channel, code, *user, "findlist") //记录列表页实际采集数据量心跳
+				s.RecordedHeartInfo = true
+			}
 		}
 		//}
 		return 1

+ 26 - 36
src/spider/spider.go

@@ -270,10 +270,11 @@ func (s *Spider) DownListPageItem() (errs interface{}) {
 	defer mu.Catch()
 	s.AlreadyGetPageHeart = map[int]bool{}                                     //重置记录
 	start, max := s.GetIntVar("spiderStartPage"), s.GetIntVar("spiderMaxPage") //起始页、最大页
-	tmpMax := max                                                              //临时记录最大页
-	repeatAllNum := 0                                                          //本轮采集tmpMax页总的重复个数
-	downloadAllNum := 0                                                        //本轮采集tmpMax页总个数
-	if util.Config.IsHistoryEvent && s.GetVar("spiderType") == "history" {     //7000节点,爬虫跑历史
+	s.MaxPage = max                                                            //
+	//tmpMax := max                                                              //临时记录最大页
+	repeatAllNum := 0                                                      //本轮采集tmpMax页总的重复个数
+	downloadAllNum := 0                                                    //本轮采集tmpMax页总个数
+	if util.Config.IsHistoryEvent && s.GetVar("spiderType") == "history" { //7000节点,爬虫跑历史
 		max = s.GetIntVar("spiderHistoryMaxPage")
 	}
 	downtimes := 0                                                                     //记录某页重试次数(暂定3次)
@@ -289,11 +290,8 @@ func (s *Spider) DownListPageItem() (errs interface{}) {
 			max = 50                 //队列模式最大页50
 		}
 	}
-	inc := map[string]interface{}{}
-	pageOneDataHash := ""
 	for ; start <= max && !s.Stop; start++ {
-		var HrefArr []string //记录href
-		if !s.Stop {         //在下载详情页时爬虫下架,此时不再存心跳信息
+		if !s.Stop { //在下载详情页时爬虫下架,此时不再存心跳信息
 			UpdateHeart(s.Name, s.Channel, s.Code, s.MUserName, "list") //记录所有节点列表页心跳
 		}
 		//logger.Info("爬虫:", s.Code, "重复页:", repeatPageNum, "	配置最大页:", tmpMax, "	最终最大页:", max, "	当前页:", start, "重复次数:", repeatPageTimes)
@@ -338,9 +336,6 @@ func (s *Spider) DownListPageItem() (errs interface{}) {
 				for i := 1; i <= tabLen; i++ {
 					v := tbl.RawGetInt(i).(*lua.LTable)
 					tmp := util.TableToMap(v)
-					if tmpMax > 1 { //配置页码大于1时记录
-						HrefArr = append(HrefArr, qu.ObjToString(tmp["href"]))
-					}
 					//s.ThisSiteData(tmp)      //统计当前下载数据是否是本站点数据
 					if !s.IsHistoricalMend { //不是历史补漏
 						tmp["dataging"] = 0 //数据中打标记dataging=0
@@ -417,28 +412,8 @@ func (s *Spider) DownListPageItem() (errs interface{}) {
 			}
 		}
 		downtimes = 0 //当前页下载无误,重置下载重试次数
-		//记录翻页异常爬虫(目前只统计页面至少能翻到第二页,且两页都能采到数据的情况。)
-		if tmpMax > 1 && !util.Config.IsHistoryEvent {
-			if start == 1 && len(HrefArr) > 0 { //记录第一页数据的hash
-				pageOneDataHash = util.HexText(strings.Join(HrefArr, ","))
-			} else if start == 2 && pageOneDataHash != "" { //对比第一页的hash
-				//此处hrefArr可能为空,1、可能全部过滤;2、采集失败无数据
-				if len(HrefArr) > 0 {
-					pageTwoDataHash := util.HexText(strings.Join(HrefArr, ","))
-					if pageTwoDataHash == pageOneDataHash {
-						inc["page_fail"] = 1 //两页数据相同,翻页失败
-					} else { //两页数据不同,翻页成功
-						inc["page_success"] = 1
-					}
-				} else { //第二页无数据,无法翻页
-					inc["page_fail"] = 1
-				}
-			}
-		}
-		HrefArr = []string{}
 		util.TimeSleepFunc(100*time.Millisecond, TimeSleepChan)
 	}
-	pageOneDataHash = ""
 	logger.Info(s.Code, "本轮列表页采集详情:", downloadAllNum, repeatAllNum, start, s.Stop)
 	if !util.Config.IsHistoryEvent && !s.Stop { //非历史节点统计下载率
 		nowTime := time.Now()
@@ -450,15 +425,26 @@ func (s *Spider) DownListPageItem() (errs interface{}) {
 			"updatetime": nowTime.Unix(),
 			"event":      util.Config.Uploadevent,
 			"modifyuser": s.MUserName,
-			"maxpage":    tmpMax,
+			"maxpage":    s.MaxPage,
 			"runrate":    s.SpiderRunRate,
 			"endpage":    start,
 			"date":       sDate,
 		}
-		//inc = map[string]interface{}{
-		//	"alltimes": 1,
-		//}
-		inc["alltimes"] = 1
+		inc := map[string]interface{}{
+			"alltimes": 1,
+		}
+		//记录翻页是否成功
+		if s.PageOneTextHash != "" {
+			if s.PageTwoTextHash != "" {
+				if s.PageOneTextHash != s.PageTwoTextHash {
+					inc["page_success"] = 1
+				} else {
+					inc["page_fail"] = 1
+				}
+			} else {
+				inc["page_fail"] = 1
+			}
+		}
 		if downloadAllNum > 0 {
 			rate := float64(downloadAllNum-repeatAllNum) / float64(downloadAllNum)
 			rate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", rate), 64)
@@ -483,6 +469,10 @@ func (s *Spider) DownListPageItem() (errs interface{}) {
 			"$inc": inc,
 		}, true, false)
 	}
+	//信息重置
+	s.RecordedHeartInfo = false
+	s.PageOneTextHash = ""
+	s.PageTwoTextHash = ""
 	return errs
 }