check.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package vm
  2. import (
  3. "container/list"
  4. "errors"
  5. "fmt"
  6. qu "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  7. be "spider_creator/backend"
  8. "time"
  9. "github.com/chromedp/chromedp"
  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, false, false) //详情页使用
  19. // defer func() {
  20. // incCancelFn2()
  21. // baseCancelFn2()
  22. // incCancelFn()
  23. // baseCancelFn()
  24. // }()
  25. //
  26. // listRunJs, contentRunJs := sc.ListJSCode, sc.ContentJSCode
  27. // //2. 执行JS代码,获取列表页信息
  28. // if be.RegSpace.ReplaceAllString(listRunJs, "") == "" {
  29. // listRunJs = renderJavascriptCoder(loadListItemsJS, sc)
  30. // }
  31. // if be.RegSpace.ReplaceAllString(contentRunJs, "") == "" {
  32. // contentRunJs = renderJavascriptCoder(loadContentJS, sc)
  33. // }
  34. // qu.Debug("获取列表页JS代码:", listRunJs)
  35. // qu.Debug("获取详情页JS代码:", contentRunJs)
  36. // //3.打开列表,获取条目清单
  37. // chromedp.Run(ctx, chromedp.Tasks{
  38. // chromedp.Navigate(sc.Href),
  39. // chromedp.WaitReady("document.body", chromedp.ByJSPath),
  40. // //chromedp.Sleep(1000 * time.Millisecond),
  41. // chromedp.Sleep(time.Duration(sc.ListDelayTime) * time.Millisecond),
  42. // })
  43. // //初始化列表页信息
  44. // if !vm.InitListPage(ctx, sc) {
  45. // qu.Debug("初始化列表页失败,退出")
  46. // return ret, errors.New("初始化列表页失败")
  47. // }
  48. // no := 1
  49. //T:
  50. // for j := 0; j < 2; 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. // if sc.AttachCss != "" {
  88. // downloadAttaches(r, vm.attachesDir)
  89. // }
  90. // r.Site = sc.Site
  91. // r.Channel = sc.Channel
  92. // if r.Title == "" {
  93. // r.Title = r.ListTitle
  94. // }
  95. // if r.PublishTime == "" {
  96. // r.PublishTime = r.ListPubTime
  97. // }
  98. // r.No = no
  99. // no += 1
  100. // //结果放入缓存
  101. // verifyResult.PushBack(r)
  102. // }
  103. // qu.Debug("第"+fmt.Sprint(j+1)+"页校验成功数据条数:", verifyResult.Len())
  104. // //翻页
  105. // if verifyResult.Len() > 0 {
  106. // if sc.MaxPages == 1 { //最大页为1,不校验翻页
  107. // ret.ListTrunPage = true
  108. // break
  109. // } else if sc.MaxPages > 1 { //&& !ret.ListTrunPage {
  110. // if err = trunPage(sc, sc.ListTurnDelayTime, ctx); err != nil { //翻页失败
  111. // qu.Debug("第" + fmt.Sprint(j+1) + "页翻页失败")
  112. // break T
  113. // } else {
  114. // ret.ListTrunPage = true
  115. // }
  116. // }
  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 != ""
  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. listRunJs, contentRunJs := sc.ListJSCode, sc.ContentJSCode
  144. //2. 执行JS代码,获取列表页信息
  145. if be.RegSpace.ReplaceAllString(listRunJs, "") == "" {
  146. listRunJs = renderJavascriptCoder(loadListItemsJS, sc)
  147. }
  148. if be.RegSpace.ReplaceAllString(contentRunJs, "") == "" {
  149. contentRunJs = renderJavascriptCoder(loadContentJS, sc)
  150. }
  151. qu.Debug("列表页JS:", listRunJs)
  152. //3.打开列表,获取条目清单
  153. chromedp.Run(ctx, chromedp.Tasks{
  154. chromedp.Navigate(sc.Href),
  155. chromedp.WaitReady("document.body", chromedp.ByJSPath),
  156. //chromedp.Sleep(1000 * time.Millisecond),
  157. chromedp.Sleep(time.Duration(sc.ListDelayTime) * time.Millisecond),
  158. })
  159. //4.初始化列表页信息
  160. if !vm.InitListPage(ctx, sc) {
  161. qu.Debug("初始化列表页失败,退出")
  162. return ret, errors.New("初始化列表页失败")
  163. }
  164. no := 1
  165. T:
  166. for j := 0; j < 2; j++ { //最多检查2页
  167. qu.Debug("开始检查第" + fmt.Sprint(j+1) + "页...")
  168. listResult := make(be.ResultItems, 0)
  169. err := chromedp.Run(ctx, chromedp.Tasks{
  170. chromedp.Evaluate(listRunJs, &listResult),
  171. })
  172. if err != nil {
  173. qu.Debug("执行列表页JS代码失败", err.Error())
  174. continue
  175. }
  176. //5.操作详情页
  177. qu.Debug("列表采集条数:", len(listResult))
  178. for contentIndex, r := range listResult {
  179. if contentIndex > 1 { //每页只校验2条
  180. break
  181. }
  182. qu.Debug("当前列表页第" + fmt.Sprint(contentIndex+1) + "条")
  183. r.Site = sc.Site
  184. r.Channel = sc.Channel
  185. //qu.Debug(r.Title, r.ListTitle)
  186. if r.Title == "" {
  187. r.Title = r.ListTitle
  188. }
  189. //qu.Debug(r.PublishTime, r.ListPubTime)
  190. if r.PublishTime == "" {
  191. r.PublishTime = r.ListPubTime
  192. }
  193. r.No = no
  194. no += 1
  195. //结果放入缓存
  196. verifyResult.PushBack(r)
  197. }
  198. qu.Debug("列表采集条数结果:", verifyResult.Len())
  199. //6.翻页
  200. if verifyResult.Len() > 0 {
  201. if sc.MaxPages == 1 { //最大页为1,不校验翻页
  202. ret.ListTrunPage = true
  203. break
  204. } else if sc.MaxPages > 1 { // && !ret.ListTrunPage {
  205. if err = trunPage(sc, sc.ListTurnDelayTime, ctx); err != nil { //翻页失败
  206. qu.Debug("翻页失败:", err)
  207. break T
  208. } else {
  209. ret.ListTrunPage = true
  210. }
  211. }
  212. }
  213. }
  214. //检查
  215. for el := verifyResult.Front(); el != nil; el = el.Next() {
  216. r, _ := el.Value.(*be.ResultItem)
  217. ret.Title = r.Title != ""
  218. qu.Debug("Check Title:", ret.Title, r.Title, r.ListTitle)
  219. ret.PublishTime = r.PublishTime != ""
  220. qu.Debug("Check PublishTime:", ret.PublishTime, r.PublishTime, r.ListPubTime)
  221. }
  222. if ret.ListItems {
  223. ret.ListItems = (sc.MaxPages == 1 && verifyResult.Len() > 0) || (sc.MaxPages > 1 && verifyResult.Len() > 2)
  224. }
  225. return ret, nil
  226. }