check.go 6.7 KB

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