SSQGlossaryJob.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.LotteryGlossary
  11. {
  12. public class SSQGlossaryJob : IJob
  13. {
  14. /// <summary>
  15. /// 构造函数
  16. /// </summary>
  17. public SSQGlossaryJob()
  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<LotteryGlossaryModel> res = GetOpenListFromMainUrl(Config.MainUrl);
  38. foreach (var LotteryGlossaryModel in res)
  39. {
  40. if (services.LotteryGlossaryModel(currentLottery, LotteryGlossaryModel))
  41. {
  42. //Do Success Log
  43. log.Info(GetType(), CommonHelper.GetJobMainLogInfo(Config, LotteryGlossaryModel.Title));
  44. isGetData = true;
  45. }
  46. }
  47. }
  48. /// <summary>
  49. /// 爬取主站技巧列表
  50. /// </summary>
  51. /// <param name="mainUrl"></param>
  52. /// <returns></returns>
  53. private List<LotteryGlossaryModel> GetOpenListFromMainUrl(string mainUrl)
  54. {
  55. var result = new List<LotteryGlossaryModel>();
  56. try
  57. {
  58. var url = new Uri(mainUrl);
  59. var htmlResource = NetHelper.GetUrlResponse(mainUrl, Encoding.GetEncoding("utf-8"));
  60. if (htmlResource == null) return result;
  61. HtmlDocument doc = new HtmlDocument();
  62. doc.LoadHtml(htmlResource);
  63. //获取li下面所有a标签
  64. HtmlNodeCollection nodeList = doc.DocumentNode.SelectNodes("//*[@id='ssq']/div[3]/ul/li/a");//*[@id="ssq"]/div[3]
  65. if (nodeList == null) return result;
  66. List<string> urls = new List<string>();
  67. //遍历a标签
  68. foreach (HtmlNode node in nodeList)
  69. {
  70. HtmlAttribute attr = node.Attributes.SingleOrDefault(a => a.Name.Equals("href"));
  71. if (attr != null)
  72. {
  73. string href = Host + attr.Value;
  74. //去重
  75. if (!urls.Contains(href))
  76. {
  77. urls.Add(href);
  78. }
  79. }
  80. }
  81. foreach (var url1 in urls)
  82. {
  83. var LotterySkill = GetSkillModel(url1);
  84. result.Add(LotterySkill);
  85. }
  86. }
  87. catch (Exception ex)
  88. {
  89. log.Error(GetType(),
  90. string.Format("【{0}】通过主抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  91. }
  92. return result;
  93. }
  94. /// <summary>
  95. /// 根据主站url获取技巧详情
  96. /// </summary>
  97. /// <param name="url"></param>
  98. /// <returns></returns>
  99. private LotteryGlossaryModel GetSkillModel(string url)
  100. {
  101. LotteryGlossaryModel lotterySkill = new LotteryGlossaryModel();
  102. try
  103. {
  104. var htmlResource = NetHelper.GetUrlResponse(url, Encoding.GetEncoding("utf-8"));
  105. if (htmlResource == null) return lotterySkill;
  106. HtmlDocument doc = new HtmlDocument();
  107. doc.LoadHtml(htmlResource);
  108. //获取li下面所有a标签
  109. var div = doc.DocumentNode.SelectSingleNode("//*[@class='artile']");
  110. var Title = div.ChildNodes.Where(node => node.Name == "h1").ToList();
  111. var div1 = div.ChildNodes.Where(node => node.Name == "div").ToList();
  112. lotterySkill.Title = Title[0].InnerText.Trim();
  113. lotterySkill.Author = "cn55128";
  114. lotterySkill.Content = div1[1].InnerHtml.Trim();
  115. lotterySkill.IsDelete = false;
  116. lotterySkill.SourceUrl = url.ToString();
  117. lotterySkill.TypeID = lotterySkillType;
  118. lotterySkill.TypeName = lotterySkillType.GetEnumDescription();
  119. }
  120. catch (Exception ex)
  121. {
  122. log.Error(GetType(),
  123. string.Format("【{0}】通过主抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  124. }
  125. return lotterySkill;
  126. }
  127. #region Attribute
  128. /// <summary>
  129. /// 主机地址
  130. /// </summary>
  131. public string Host = "http://www.55125.cn/";
  132. /// <summary>
  133. /// 配置信息
  134. /// </summary>
  135. private SCCConfig Config;
  136. /// <summary>
  137. /// 当天抓取的最新一期开奖记录
  138. /// </summary>
  139. private LotteryGlossaryModel LatestItem = null;
  140. /// <summary>
  141. /// 当天抓取失败列表
  142. /// </summary>
  143. private List<string> FailedQiHaoList = null;
  144. /// <summary>
  145. /// 日志对象
  146. /// </summary>
  147. private readonly LogHelper log;
  148. /// <summary>
  149. /// 数据服务
  150. /// </summary>
  151. private readonly IDTOpenCode services;
  152. /// <summary>
  153. /// 当前彩种
  154. /// </summary>
  155. private SCCLottery currentLottery => SCCLottery.LotteryGlossary;
  156. /// <summary>
  157. /// 福彩3D技巧
  158. /// </summary>
  159. private LotteryGlossaryType lotterySkillType = LotteryGlossaryType.SSQ;
  160. /// <summary>
  161. /// 邮件接口
  162. /// </summary>
  163. private IEmail email;
  164. /// <summary>
  165. /// 是否本次运行抓取到开奖数据
  166. /// </summary>
  167. private bool isGetData = false;
  168. #endregion
  169. }
  170. }