check.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package vm
  2. import (
  3. "container/list"
  4. "errors"
  5. "fmt"
  6. "github.com/chromedp/chromedp"
  7. qu "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  8. be "spider_creator/backend"
  9. "time"
  10. )
  11. // VerifySpiderConfig 验证爬虫配置,支持翻页,列表项数据只提取2条
  12. func (vm *VM) VerifySpiderConfig(sc *be.SpiderConfig) (*be.SpiderConfigVerifyResult, error) {
  13. qu.Debug("sc---", *sc)
  14. verifyResult := list.New()
  15. be.DataResults[sc.Code] = verifyResult
  16. ret := &be.SpiderConfigVerifyResult{false, false, false, false, false, false, false}
  17. _, baseCancelFn, _, _, ctx, incCancelFn := be.NewBrowser(false, false, false) //列表页使用
  18. _, baseCancelFn2, _, _, ctx2, incCancelFn2 := be.NewBrowser(false, true, false) //详情页使用
  19. defer func() {
  20. incCancelFn2()
  21. baseCancelFn2()
  22. incCancelFn()
  23. baseCancelFn()
  24. }()
  25. listRunJs, contentRunJs := sc.ListJSCode, sc.ContentJSCode
  26. //2. 执行JS代码,获取列表页信息
  27. if be.RegSpace.ReplaceAllString(listRunJs, "") == "" {
  28. listRunJs = renderJavascriptCoder(loadListItemsJS, sc)
  29. }
  30. if be.RegSpace.ReplaceAllString(contentRunJs, "") == "" {
  31. contentRunJs = renderJavascriptCoder(loadContentJS, sc)
  32. }
  33. qu.Debug("获取列表页JS代码:", listRunJs)
  34. qu.Debug("获取详情页JS代码:", contentRunJs)
  35. //3.打开列表,获取条目清单
  36. chromedp.Run(ctx, chromedp.Tasks{
  37. chromedp.Navigate(sc.Href),
  38. chromedp.WaitReady("document.body", chromedp.ByJSPath),
  39. //chromedp.Sleep(1000 * time.Millisecond),
  40. chromedp.Sleep(time.Duration(sc.ListDelayTime) * time.Millisecond),
  41. })
  42. //初始化列表页信息
  43. if !vm.InitListPage(ctx, sc) {
  44. qu.Debug("初始化列表页失败,退出")
  45. return ret, errors.New("初始化列表页失败")
  46. }
  47. no := 1
  48. ret.ListTrunPage = true
  49. T:
  50. for j := 0; j < VERIVY_MAX_TRUN_PAGE && j < int(sc.MaxPages); j++ { //最多检查2页
  51. qu.Debug("开始检查第" + fmt.Sprint(j+1) + "页...")
  52. listResult := make(be.ResultItems, 0)
  53. err := chromedp.Run(ctx, chromedp.Tasks{
  54. chromedp.Evaluate(listRunJs, &listResult),
  55. })
  56. if err != nil {
  57. qu.Debug("执行列表页JS代码失败", err.Error())
  58. continue
  59. }
  60. //TODO 5.操作详情页
  61. qu.Debug("列表采集条数:", len(listResult))
  62. for contentIndex, r := range listResult {
  63. qu.Debug("当前列表页第" + fmt.Sprint(contentIndex+1) + "条")
  64. if contentIndex > 1 { //每页只校验2条
  65. break
  66. }
  67. //打开详情页
  68. err = chromedp.Run(ctx2, chromedp.Tasks{
  69. chromedp.Navigate(r.Href),
  70. chromedp.WaitReady("document.body", chromedp.ByJSPath),
  71. //chromedp.Sleep(2000 * time.Millisecond),
  72. chromedp.Sleep(time.Duration(sc.ContentDelayTime) * time.Millisecond),
  73. })
  74. if err != nil {
  75. qu.Debug("当前列表页第" + fmt.Sprint(contentIndex+1) + "条详情页打开异常")
  76. continue
  77. }
  78. //获取详情页内容
  79. err = chromedp.Run(ctx2, chromedp.Tasks{
  80. chromedp.Evaluate(contentRunJs, r),
  81. })
  82. if err != nil {
  83. qu.Debug("当前列表页第" + fmt.Sprint(contentIndex+1) + "条详情页内容获取失败")
  84. continue
  85. }
  86. //下载附件
  87. qu.Debug(r.Title, r.ListTitle)
  88. if sc.AttachCss != "" {
  89. downloadAttaches(r, vm.attachesDir)
  90. }
  91. r.Site = sc.Site
  92. r.Channel = sc.Channel
  93. if r.Title == "" {
  94. r.Title = r.ListTitle
  95. }
  96. if r.PublishTime == "" {
  97. r.PublishTime = r.ListPubTime
  98. }
  99. r.No = no
  100. no += 1
  101. //结果放入缓存
  102. verifyResult.PushBack(r)
  103. }
  104. qu.Debug("第"+fmt.Sprint(j+1)+"页校验成功数据条数:", verifyResult.Len())
  105. //翻页
  106. if verifyResult.Len() > 0 {
  107. if sc.MaxPages > 1 && j < VERIVY_MAX_TRUN_PAGE-1 && j < int(sc.MaxPages)-1 { //&& !ret.ListTrunPage {
  108. if err = trunPage(sc, sc.ListTurnDelayTime, ctx); err != nil { //翻页失败
  109. qu.Debug("第" + fmt.Sprint(j+1) + "页翻页失败")
  110. ret.ListTrunPage = false
  111. break T
  112. }
  113. }
  114. } else {
  115. ret.ListTrunPage = false
  116. break T
  117. }
  118. }
  119. //检查
  120. for el := verifyResult.Front(); el != nil; el = el.Next() {
  121. r, _ := el.Value.(*be.ResultItem)
  122. ret.Title = r.Title != ""
  123. ret.PublishUnit = r.PublishUnit != ""
  124. ret.PublishTime = r.PublishTime != "" && Reg_Date.MatchString(r.PublishTime)
  125. ret.Content = r.Content != ""
  126. ret.Attaches = len(r.AttachLinks) > 0
  127. }
  128. qu.Debug(verifyResult.Len())
  129. ret.ListItems = (sc.MaxPages == 1 && verifyResult.Len() > 0) || (sc.MaxPages > 1 && verifyResult.Len() > 2)
  130. return ret, nil
  131. }
  132. // VerifySpiderConfig 只验证列表标注
  133. //func (vm *VM) VerifySpiderConfig(sc *be.SpiderConfig) (*be.SpiderConfigVerifyResult, error) {
  134. // qu.Debug("sc---", *sc)
  135. // verifyResult := list.New()
  136. // be.DataResults[sc.Code] = verifyResult
  137. // ret := &be.SpiderConfigVerifyResult{false, true, false, true, true, true, false}
  138. // _, baseCancelFn, _, _, ctx, incCancelFn := be.NewBrowser(false, false, false) //列表页使用
  139. // defer func() {
  140. // incCancelFn()
  141. // baseCancelFn()
  142. // }()
  143. //
  144. // listRunJs, contentRunJs := sc.ListJSCode, sc.ContentJSCode
  145. // //2. 执行JS代码,获取列表页信息
  146. // if be.RegSpace.ReplaceAllString(listRunJs, "") == "" {
  147. // listRunJs = renderJavascriptCoder(loadListItemsJS, sc)
  148. // }
  149. // if be.RegSpace.ReplaceAllString(contentRunJs, "") == "" {
  150. // contentRunJs = renderJavascriptCoder(loadContentJS, sc)
  151. // }
  152. // qu.Debug("列表页JS:", listRunJs)
  153. // //3.打开列表,获取条目清单
  154. // chromedp.Run(ctx, chromedp.Tasks{
  155. // chromedp.Navigate(sc.Href),
  156. // chromedp.WaitReady("document.body", chromedp.ByJSPath),
  157. // //chromedp.Sleep(1000 * time.Millisecond),
  158. // chromedp.Sleep(time.Duration(sc.ListDelayTime) * time.Millisecond),
  159. // })
  160. // //4.初始化列表页信息
  161. // if !vm.InitListPage(ctx, sc) {
  162. // qu.Debug("初始化列表页失败,退出")
  163. // return ret, errors.New("初始化列表页失败")
  164. // }
  165. // no := 1
  166. // ret.ListTrunPage = true
  167. //T:
  168. // for j := 0; j < VERIVY_MAX_TRUN_PAGE && j < int(sc.MaxPages); j++ { //最多检查2页
  169. // qu.Debug("开始检查第" + fmt.Sprint(j+1) + "页...")
  170. // listResult := make(be.ResultItems, 0)
  171. // err := chromedp.Run(ctx, chromedp.Tasks{
  172. // chromedp.Evaluate(listRunJs, &listResult),
  173. // })
  174. // if err != nil {
  175. // qu.Debug("执行列表页JS代码失败", err.Error())
  176. // continue
  177. // }
  178. // //5.操作详情页
  179. // qu.Debug("列表采集条数:", len(listResult))
  180. // for contentIndex, r := range listResult {
  181. // if contentIndex > 1 { //每页只校验2条
  182. // break
  183. // }
  184. // qu.Debug("当前列表页第" + fmt.Sprint(contentIndex+1) + "条")
  185. // r.Site = sc.Site
  186. // r.Channel = sc.Channel
  187. // //qu.Debug(r.Title, r.ListTitle)
  188. // if r.Title == "" {
  189. // r.Title = r.ListTitle
  190. // }
  191. // //qu.Debug(r.PublishTime, r.ListPubTime)
  192. // if r.PublishTime == "" {
  193. // r.PublishTime = r.ListPubTime
  194. // }
  195. // r.No = no
  196. // no += 1
  197. // //结果放入缓存
  198. // verifyResult.PushBack(r)
  199. // }
  200. // qu.Debug("列表采集条数结果:", verifyResult.Len())
  201. // //6.翻页
  202. // if verifyResult.Len() > 0 {
  203. // if sc.MaxPages > 1 && j < VERIVY_MAX_TRUN_PAGE-1 && j < int(sc.MaxPages)-1 { //&& !ret.ListTrunPage {
  204. // if err = trunPage(sc, sc.ListTurnDelayTime, ctx); err != nil { //翻页失败
  205. // qu.Debug("第" + fmt.Sprint(j+1) + "页翻页失败")
  206. // ret.ListTrunPage = false
  207. // break T
  208. // }
  209. // }
  210. // } else {
  211. // ret.ListTrunPage = false
  212. // break T
  213. // }
  214. // }
  215. // //检查
  216. // for el := verifyResult.Front(); el != nil; el = el.Next() {
  217. // r, _ := el.Value.(*be.ResultItem)
  218. // ret.Title = r.Title != ""
  219. // qu.Debug("Check Title:", ret.Title, r.Title, r.ListTitle)
  220. //
  221. // ret.PublishTime = r.PublishTime != "" && Reg_Date.MatchString(r.PublishTime)
  222. // qu.Debug("Check PublishTime:", ret.PublishTime, r.PublishTime, r.ListPubTime)
  223. // }
  224. // if ret.ListItems {
  225. // ret.ListItems = (sc.MaxPages == 1 && verifyResult.Len() > 0) || (sc.MaxPages > 1 && verifyResult.Len() > 2)
  226. // }
  227. //
  228. // return ret, nil
  229. //}