SSQLotteryNewsJob.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using HtmlAgilityPack;
  6. using Quartz;
  7. using SCC.Common;
  8. using SCC.Interface;
  9. using SCC.Models;
  10. namespace SCC.Crawler.LotteryNews
  11. {
  12. public class SSQLotteryNewsJob : IJob
  13. {
  14. /// <summary>
  15. /// 构造函数
  16. /// </summary>
  17. public SSQLotteryNewsJob()
  18. {
  19. log = new LogHelper();
  20. services = IOC.Resolve<IDTOpenCode>();
  21. email = IOC.Resolve<IEmail>();
  22. }
  23. /// <summary>
  24. /// 执行入口
  25. /// </summary>
  26. /// <param name="context"></param>
  27. public void Execute(IJobExecutionContext context)
  28. {
  29. Config = CommonHelper.GetConfigFromDataMap(context.JobDetail.JobDataMap);
  30. DoMainUrl();
  31. }
  32. /// <summary>
  33. /// 执行主站技巧
  34. /// </summary>
  35. private void DoMainUrl()
  36. {
  37. List<string> urls = GetMainUrl(Config);
  38. LotteryNewsModel lotterySkill = null;
  39. foreach (string url in urls)
  40. {
  41. List<LotteryNewsModel> res = GetOpenListFromMainUrl(url);
  42. foreach (var LotteryNewsModel in res)
  43. {
  44. if (LotteryNewsModel.Content != null)
  45. {
  46. if (services.LotteryNewsModel(currentLottery, LotteryNewsModel))
  47. {
  48. //Do Success Log
  49. log.Info(GetType(), CommonHelper.GetJobMainLogInfo(Config, LotteryNewsModel.Title));
  50. isGetData = true;
  51. }
  52. }
  53. }
  54. }
  55. }
  56. /// <summary>
  57. /// 组装主站爬取地址
  58. /// </summary>
  59. /// <param name="config"></param>
  60. /// <returns></returns>
  61. private List<string> GetMainUrl(SCCConfig config)
  62. {
  63. List<string> urlList = new List<string>();
  64. string url = config.MainUrl;
  65. int pages = config.MainUrlPages > 0 ? config.MainUrlPages : 1;
  66. for (int i = 1; i <= pages; i++)
  67. {
  68. string res;
  69. if (i == 1)
  70. {
  71. res = "http://www.zhcw.com/xinwen/caizhongxinwen-ssq/";
  72. }
  73. else
  74. {
  75. res = string.Format(url, i);
  76. }
  77. if (!urlList.Contains(res))
  78. {
  79. urlList.Add(res);
  80. }
  81. }
  82. return urlList;
  83. }
  84. /// <summary>
  85. /// 爬取主站技巧列表
  86. /// </summary>
  87. /// <param name="mainUrl"></param>
  88. /// <returns></returns>
  89. private List<LotteryNewsModel> GetOpenListFromMainUrl(string mainUrl)
  90. {
  91. var result = new List<LotteryNewsModel>();
  92. try
  93. {
  94. var url = new Uri(mainUrl);
  95. var htmlResource = NetHelper.GetUrlResponse(mainUrl, Encoding.GetEncoding("utf-8"));
  96. if (htmlResource == null) return result;
  97. HtmlDocument doc = new HtmlDocument();
  98. doc.LoadHtml(htmlResource);
  99. //获取li下面所有a标签
  100. HtmlNodeCollection nodeList = doc.DocumentNode.SelectNodes("//*[@class='Nleftbox']/ul/li/span/a");
  101. if (nodeList == null) return result;
  102. List<string> urls = new List<string>();
  103. //遍历a标签
  104. foreach (HtmlNode node in nodeList)
  105. {
  106. HtmlAttribute attr = node.Attributes.SingleOrDefault(a => a.Name.Equals("href"));
  107. if (attr != null)
  108. {
  109. string href = Host + attr.Value;
  110. //去重
  111. if (!urls.Contains(href))
  112. {
  113. urls.Add(href);
  114. }
  115. }
  116. }
  117. foreach (var url1 in urls)
  118. {
  119. var LotterySkill = GetSkillModel(url1);
  120. result.Add(LotterySkill);
  121. }
  122. }
  123. catch (Exception ex)
  124. {
  125. log.Error(GetType(),
  126. string.Format("【{0}】通过主抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  127. }
  128. return result;
  129. }
  130. /// <summary>
  131. /// 根据主站url获取技巧详情
  132. /// </summary>
  133. /// <param name="url"></param>
  134. /// <returns></returns>
  135. private LotteryNewsModel GetSkillModel(string url)
  136. {
  137. LotteryNewsModel lotterySkill = new LotteryNewsModel();
  138. try
  139. {
  140. var htmlResource = NetHelper.GetUrlResponse(url, Encoding.GetEncoding("utf-8"));
  141. if (htmlResource == null) return lotterySkill;
  142. HtmlDocument doc = new HtmlDocument();
  143. doc.LoadHtml(htmlResource);
  144. //获取li下面所有a标签
  145. var div = doc.DocumentNode.SelectSingleNode("//*[@class='news_content']");
  146. var Title = div.ChildNodes.Where(node => node.Name == "h2").ToList();
  147. var div1 = div.ChildNodes.Where(node => node.Name == "div").ToList();
  148. string txt = div1[2].InnerHtml.Trim();
  149. var Content = txt.Replace("<img src=\"", " <img src=\"http://www.zhcw.com").Replace("中彩网讯", "").Replace("中彩网综合报道", "综合报道").Replace("中彩网", "");
  150. if (Content == "")
  151. {
  152. Content= null;
  153. }
  154. lotterySkill.Title = Title[0].InnerText.Trim();
  155. lotterySkill.Author = "cn55128";
  156. lotterySkill.Content = Content;
  157. lotterySkill.IsDelete = false;
  158. lotterySkill.SourceUrl = url.ToString();
  159. lotterySkill.TypeID = lotterySkillType;
  160. lotterySkill.TypeName = lotterySkillType.GetEnumDescription();
  161. }
  162. catch (Exception ex)
  163. {
  164. log.Error(GetType(),
  165. string.Format("【{0}】通过主抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  166. }
  167. return lotterySkill;
  168. }
  169. #region Attribute
  170. /// <summary>
  171. /// 主机地址
  172. /// </summary>
  173. public string Host = "http://www.zhcw.com";
  174. /// <summary>
  175. /// 配置信息
  176. /// </summary>
  177. private SCCConfig Config;
  178. /// <summary>
  179. /// 当天抓取的最新一期开奖记录
  180. /// </summary>
  181. private LotteryNewsModel LatestItem = null;
  182. /// <summary>
  183. /// 当天抓取失败列表
  184. /// </summary>
  185. private List<string> FailedQiHaoList = null;
  186. /// <summary>
  187. /// 日志对象
  188. /// </summary>
  189. private readonly LogHelper log;
  190. /// <summary>
  191. /// 数据服务
  192. /// </summary>
  193. private readonly IDTOpenCode services;
  194. /// <summary>
  195. /// 当前彩种
  196. /// </summary>
  197. private SCCLottery currentLottery => SCCLottery.LotteryNews;
  198. /// <summary>
  199. /// 福彩3D技巧
  200. /// </summary>
  201. private LotteryNewsType lotterySkillType = LotteryNewsType.SSQNews;
  202. /// <summary>
  203. /// 邮件接口
  204. /// </summary>
  205. private IEmail email;
  206. /// <summary>
  207. /// 是否本次运行抓取到开奖数据
  208. /// </summary>
  209. private bool isGetData = false;
  210. #endregion
  211. }
  212. }