spider_test.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. --引用公用包
  2. local com=require "res.util.comm"
  3. --名称
  4. spiderName="中央采购网-测试脚本";
  5. --代码
  6. spiderCode="cn_cgw_test";
  7. --是否下载3级页
  8. spiderDownDetailPage=true;
  9. --开始下载页
  10. spiderStartPage=1;
  11. --最大下载也
  12. spiderMaxPage=5;
  13. --上次下载时间
  14. spiderLastDownloadTime="2015-01-01 01:10:01";
  15. --执行频率30分钟
  16. spiderRunRate=10;
  17. --下载内容写入表名
  18. spider2Collection="test111";
  19. --下载页面时使用的编码
  20. spiderPageEncoding="utf8";
  21. --是否使用代理
  22. spiderUserProxy=false;
  23. --是否是安全协议
  24. spiderUserHttps=false;
  25. --下载详细页线程数
  26. spiderThread=1
  27. --存储模式 1 直接存储,2 调用消息总线 ...
  28. spiderStoreMode=1
  29. spiderStoreToMsgEvent=4002 --消息总线event
  30. --判重字段 空默认不判重,spiderCoverAttr="title" 按title判重覆盖
  31. spiderCoverAttr="title"
  32. --延时毫秒 基本延时(spiderSleepBase)+随机延时(spiderSleepRand)
  33. spiderSleepBase=1000
  34. spiderSleepRand=1000
  35. --取得对方网站最后发布时间 必须返回yyyy-MM-dd HH:mm:ss 格式
  36. function getLastPublishTime()
  37. --transCode("unicode","内容")--转码,支持unicode,urlcode,decode64
  38. --timeSleep(5)--延时
  39. --changeDownloader()--指定下载点
  40. local content = download("http://www.ccgp.gov.cn/zycg/zycgdt/",{})
  41. local tmp = findOneText("ul>li>em:eq(0)",content)
  42. local lastpushtime=com.parseDate(tmp,"yyyyMMddHHmm")
  43. print("lastpushtime:"..lastpushtime);
  44. return lastpushtime
  45. end
  46. --下载分析列表页
  47. function downloadAndParseListPage(pageno)
  48. local page={}
  49. local href=""
  50. if pageno==1 then
  51. href="http://www.ccgp.gov.cn/zycg/zycgdt/index.htm"
  52. else
  53. href="http://www.ccgp.gov.cn/zycg/zycgdt/index_"..tostring(pageno-1)..".htm"
  54. end
  55. --print("href:"..href)
  56. local content = download(href,{})
  57. local list = findListHtml("ul#main_list_lt_list>li",content)
  58. --print("list:"..content.."content")
  59. --根据实际情况:验证下载列表内容是否正确
  60. if table.getn(list)<1 then
  61. return downloadAndParseListPage(pageno)
  62. end
  63. for k, v in pairs(list) do
  64. --分析列表,可加入自己分析列表是,需要的其他字段,最终会存储到新闻上
  65. item={}
  66. item["href"]="a:eq(1):attr(href)"
  67. item["title"]="a:eq(1):attr(title)"
  68. item["publishtime"]="em:eq(0)"
  69. item["department"]="a:eq(0):attr(title)"
  70. item=findMap(item,v)
  71. item["publishtime"]=com.parseDate(item["publishtime"],"yyyyMMddHHmm")
  72. item["href"]="http://www.ccgp.gov.cn/zycg/zycgdt/"..item["href"]
  73. page[k]=item
  74. end
  75. return page
  76. end
  77. --下载三级页,分析三级页
  78. function downloadDetailPage(data)
  79. for i=1,5 do --5次下载任务不成功,退出
  80. local content = download(data["href"],{})
  81. --print("content",content)
  82. local ret={
  83. ["sitename"]="标网",
  84. ["channel"]="招标公告",
  85. ["href"]=data["href"],
  86. ["title"]=findOneText("div.vT_detail_header h2",content),
  87. ["detail"]=findOneText("div.TRS_Editor",content),
  88. ["contenthtml"]=findOneHtml("div.TRS_Editor",content),
  89. ["publishtime"]=data["publishtime"],
  90. ["l_np_publishtime"]=com.strToTimestamp(data["publishtime"]),
  91. ["_d"]="comeintime"
  92. }
  93. local checkAttr={"title","href","publishtime","detail","contenthtml"}
  94. local b,err=com.checkData(checkAttr,ret)
  95. print(ret.href,ret.title,ret.publishtime)
  96. --os.exit()
  97. if b then
  98. return ret
  99. else
  100. --print("第",i,"次下载失败")
  101. timeSleep(60)--延时60秒再次请求
  102. if i==5 then
  103. saveErrLog(spiderCode,spiderName,ret["href"],err)
  104. end
  105. end
  106. end
  107. end
  108. --保存错误日志
  109. --saveErrLog(spiderCode,spiderName,出错url,出错原因)