check.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 verifyResult.Len() > 0 {
  98. // if sc.MaxPages == 1 { //最大页为1,不校验翻页
  99. // ret.ListTrunPage = true
  100. // break
  101. // } else if sc.MaxPages > 1 { //&& !ret.ListTrunPage {
  102. // if err = trunPage(sc, sc.ListTurnDelayTime, ctx); err != nil { //翻页失败
  103. // break T
  104. // } else {
  105. // ret.ListTrunPage = true
  106. // }
  107. // }
  108. // }
  109. // }
  110. // //检查
  111. // for el := verifyResult.Front(); el != nil; el = el.Next() {
  112. // r, _ := el.Value.(*be.ResultItem)
  113. // if ret.Title {
  114. // ret.Title = r.Title != ""
  115. // }
  116. // if ret.PublishUnit {
  117. // ret.PublishUnit = r.PublishUnit != ""
  118. // }
  119. // if ret.PublishTime {
  120. // ret.PublishTime = r.PublishTime != ""
  121. // }
  122. // if ret.Content {
  123. // ret.Content = r.Content != ""
  124. // }
  125. // if ret.Attaches {
  126. // ret.Attaches = len(r.AttachLinks) > 0
  127. // }
  128. // }
  129. // qu.Debug(verifyResult.Len())
  130. // if ret.ListItems {
  131. // ret.ListItems = (sc.MaxPages == 1 && verifyResult.Len() > 0) || (sc.MaxPages > 1 && verifyResult.Len() > 2)
  132. // }
  133. //
  134. // //TODO:每次验证结果存库、内存?
  135. // return ret, nil
  136. //}
  137. // VerifySpiderConfig 只验证列表标注
  138. func (vm *VM) VerifySpiderConfig(sc *be.SpiderConfig) (*be.SpiderConfigVerifyResult, error) {
  139. qu.Debug("sc---", *sc)
  140. verifyResult := list.New()
  141. ret := &be.SpiderConfigVerifyResult{false, true, false, true, true, true, false}
  142. _, baseCancelFn, _, _, ctx, incCancelFn := be.NewBrowser(false, false, "") //列表页使用
  143. defer func() {
  144. incCancelFn()
  145. baseCancelFn()
  146. }()
  147. listRunJs, contentRunJs := sc.ListJSCode, sc.ContentJSCode
  148. //TODO 2. 执行JS代码,获取列表页信息
  149. if be.RegSpace.ReplaceAllString(listRunJs, "") == "" {
  150. listRunJs = renderJavascriptCoder(loadListItemsJS, sc)
  151. }
  152. if be.RegSpace.ReplaceAllString(contentRunJs, "") == "" {
  153. contentRunJs = renderJavascriptCoder(loadContentJS, sc)
  154. }
  155. qu.Debug("列表页JS:", listRunJs)
  156. //TODO 3.打开列表,获取条目清单
  157. chromedp.Run(ctx, chromedp.Tasks{
  158. chromedp.Navigate(sc.Href),
  159. chromedp.WaitReady("document.body", chromedp.ByJSPath),
  160. //chromedp.Sleep(1000 * time.Millisecond),
  161. chromedp.Sleep(time.Duration(sc.ListDelayTime) * time.Millisecond),
  162. })
  163. no := 1
  164. T:
  165. for j := 0; j < 2; j++ { //最多检查2页
  166. qu.Debug("开始检查第" + fmt.Sprint(j+1) + "页...")
  167. listResult := make(be.ResultItems, 0)
  168. err := chromedp.Run(ctx, chromedp.Tasks{
  169. chromedp.Evaluate(listRunJs, &listResult),
  170. })
  171. if err != nil {
  172. qu.Debug("执行列表页JS代码失败", err.Error())
  173. continue
  174. }
  175. //TODO 5.操作详情页
  176. qu.Debug("列表采集条数:", len(listResult))
  177. for contentIndex, r := range listResult {
  178. if contentIndex > 1 { //每页只校验2条
  179. break
  180. }
  181. qu.Debug("当前列表页第" + fmt.Sprint(contentIndex+1) + "条")
  182. r.Site = sc.Site
  183. r.Channel = sc.Channel
  184. qu.Debug(r.Title, r.ListTitle)
  185. if r.Title == "" {
  186. r.Title = r.ListTitle
  187. }
  188. qu.Debug(r.PublishTime, r.ListPubTime)
  189. if r.PublishTime == "" {
  190. r.PublishTime = r.ListPubTime
  191. }
  192. r.No = no
  193. no += 1
  194. //结果放入缓存
  195. verifyResult.PushBack(r)
  196. }
  197. qu.Debug("列表采集条数结果:", verifyResult.Len())
  198. //TODO 6.翻页
  199. if verifyResult.Len() > 0 {
  200. if sc.MaxPages == 1 { //最大页为1,不校验翻页
  201. ret.ListTrunPage = true
  202. break
  203. } else if sc.MaxPages > 1 { // && !ret.ListTrunPage {
  204. if err = trunPage(sc, sc.ListTurnDelayTime, ctx); err != nil { //翻页失败
  205. qu.Debug("翻页失败:", err)
  206. break T
  207. } else {
  208. ret.ListTrunPage = true
  209. }
  210. }
  211. }
  212. }
  213. //检查
  214. for el := verifyResult.Front(); el != nil; el = el.Next() {
  215. r, _ := el.Value.(*be.ResultItem)
  216. ret.Title = r.Title != ""
  217. qu.Debug("Check Title:", ret.Title, r.Title, r.ListTitle)
  218. ret.PublishTime = r.PublishTime != ""
  219. qu.Debug("Check PublishTime:", ret.PublishTime, r.PublishTime, r.ListPubTime)
  220. }
  221. if ret.ListItems {
  222. ret.ListItems = (sc.MaxPages == 1 && verifyResult.Len() > 0) || (sc.MaxPages > 1 && verifyResult.Len() > 2)
  223. }
  224. return ret, nil
  225. }