YJ_FootBallNewsJob.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using FCS.Common;
  2. using FCS.Crawler.Tools;
  3. using FCS.Interface;
  4. using FCS.Models;
  5. using HtmlAgilityPack;
  6. using Newtonsoft.Json;
  7. using Quartz;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. namespace FCS.Crawler.ZCLotteryNews
  15. {
  16. /// <summary>
  17. /// 意甲新闻
  18. /// </summary>
  19. public class YJ_FootBallNewsJob : CommonJob, IJob
  20. {
  21. public YJ_FootBallNewsJob()
  22. {
  23. log = new LogHelper();
  24. services = IOC.Resolve<IDTOpenCode>();
  25. }
  26. public void Execute(IJobExecutionContext context)
  27. {
  28. Config = CommonHelper.GetConfigFromDataMap(context.JobDetail.JobDataMap);
  29. GetAll();
  30. }
  31. /// <summary>
  32. /// 执行主站技巧
  33. /// </summary>
  34. public void GetAll()
  35. {
  36. List<string> urls = new List<string> { "http://sports.163.com/yj/" };
  37. foreach (string url in urls)
  38. {
  39. //获取英超的新闻列表,
  40. List<Base_News> yc_news = GetOpenListFromMainUrl(url);
  41. foreach (var newItem in yc_news)
  42. {
  43. services.AddNews(currentNews, newItem);
  44. }
  45. }
  46. }
  47. /// <summary>
  48. /// 爬取网易的新闻列表
  49. /// </summary>
  50. /// <param name="mainUrl"></param>
  51. /// <returns></returns>
  52. private List<Base_News> GetOpenListFromMainUrl(string mainUrl)
  53. {
  54. var result = new List<Base_News>();
  55. try
  56. {
  57. var url = new Uri(mainUrl);
  58. var htmlResource = NetHelper.GetUrlResponse(mainUrl, Encoding.GetEncoding("gbk"));
  59. if (htmlResource == null) return result;
  60. HtmlDocument doc = new HtmlDocument();
  61. doc.LoadHtml(htmlResource);
  62. var dataurl = "http://sports.163.com/special/000587PN/newsdata_world_yj.js?callback=data_callback";
  63. var html = CommonMethod.HttpJs(dataurl);
  64. //TODO 目前只获取到网页中的头条新闻,其他新闻还在上面这个js文件中,待查看文件夹不是每天生成
  65. //遍历div下的a标签
  66. HtmlNodeCollection nodeList = doc.DocumentNode.SelectNodes("//*[@class='topnews']/h2/a");
  67. if (nodeList == null) return result;
  68. List<string> urls = new List<string>();
  69. //遍历a标签
  70. foreach (HtmlNode node in nodeList)
  71. {
  72. HtmlAttribute attr = node.Attributes.SingleOrDefault(a => a.Name.Equals("href"));
  73. if (attr != null)
  74. {
  75. string href = attr.Value;
  76. //去重
  77. if (!urls.Contains(href))
  78. {
  79. urls.Add(href);
  80. }
  81. }
  82. }
  83. //爬取新闻主题
  84. foreach (var url1 in urls)
  85. {
  86. var YCNews = GetNewsModel(url1);
  87. if (YCNews.FullHead != null && YCNews.FullHead != "")
  88. {
  89. result.Add(YCNews);
  90. }
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. log.Error(GetType(),
  96. string.Format("【{0}】通过主抓取意甲新闻时发生错误,错误信息【{1}】", Config.Area + currentNews, ex.Message));
  97. }
  98. return result;
  99. }
  100. /// <summary>
  101. /// 获取新闻的主题内容
  102. /// </summary>
  103. /// <param name="url"></param>
  104. /// <returns></returns>
  105. private Base_News GetNewsModel(string url)
  106. {
  107. Base_News YCNew = new Base_News();
  108. try
  109. {
  110. var htmlResource = NetHelper.GetUrlResponse(url, Encoding.GetEncoding("gb2312"));
  111. if (htmlResource == null) return YCNew;
  112. HtmlDocument doc = new HtmlDocument();
  113. doc.LoadHtml(htmlResource);
  114. var div = doc.DocumentNode.SelectSingleNode("//*[@class='post_content_main']");
  115. if (div == null) return YCNew;
  116. var Title = div.ChildNodes.Where(node => node.Name == "h1").ToList();
  117. var divContent = doc.DocumentNode.SelectSingleNode("//*[@class='post_text']");
  118. if (divContent == null) return YCNew;
  119. string NewContent = divContent.InnerHtml.Trim();
  120. if (NewContent == "")
  121. {
  122. NewContent = null;
  123. }
  124. var timeDiv = doc.DocumentNode.SelectSingleNode("//*[@class='post_time_source']");
  125. YCNew.ReleaseTime = timeDiv.FirstChild.InnerText.Replace("来源:", "").Replace("网易体育", "").Replace("\n", "");
  126. HtmlDocument imgdoc = new HtmlDocument();
  127. imgdoc.LoadHtml(NewContent);
  128. var img = imgdoc.DocumentNode.SelectSingleNode("//*[@class='f_center']/img");
  129. if (img != null)
  130. {
  131. var imgsrc = img.Attributes.SingleOrDefault(a => a.Name.Equals("src"));
  132. YCNew.SourceAddress = imgsrc.Value;
  133. }
  134. YCNew.Id = Guid.NewGuid().ToString();
  135. YCNew.FullHead = Title[0].InnerText.Trim();
  136. YCNew.AuthorName = "zc55128";
  137. YCNew.NewsContent = NoHTML(NewContent);
  138. YCNew.SourceName = "网易体育 意甲";
  139. YCNew.TypeId = (int)NewsTypeEnum.意甲;
  140. var sql = string.Format(GetLotterySqlByTableName, "Base_DataItemDetail", currentNews.GetEnumDescription());
  141. var res = SqlHelper.ExecuteDataset(CommandType.Text, sql);
  142. if (res != null && res.Tables.Count > 0 && res.Tables[0].Rows.Count > 0)
  143. {
  144. YCNew.CategoryId = res.Tables[0].Rows[0]["Id"].ToString();
  145. }
  146. YCNew.Category = currentNews.GetEnumDescription();
  147. YCNew.CreateDate = DateTime.Now;
  148. }
  149. catch (Exception ex)
  150. {
  151. log.Error(GetType(),
  152. string.Format("【{0}】通过主抓取意甲新闻时发生错误,错误信息【{1}】", Config.Area + currentNews, ex.Message));
  153. }
  154. return YCNew;
  155. }
  156. /// <summary>
  157. /// 组装主站爬取地址
  158. /// </summary>
  159. /// <param name="config"></param>
  160. /// <returns></returns>
  161. private List<string> GetMainUrl(FCSConfig config)
  162. {
  163. List<string> urlList = new List<string>();
  164. string url = config.MainUrl;
  165. int pages = config.MainUrlPages > 0 ? config.MainUrlPages : 1;
  166. for (int i = 1; i <= pages; i++)
  167. {
  168. string res;
  169. if (i == 1)
  170. {
  171. res = "http://sports.163.com/yj/";
  172. }
  173. else
  174. {
  175. res = string.Format(url, i);
  176. }
  177. if (!urlList.Contains(res))
  178. {
  179. urlList.Add(res);
  180. }
  181. }
  182. return urlList;
  183. }
  184. public static string NoHTML(string html) //去除HTML标记
  185. {
  186. Regex regex1 =
  187. new Regex(@"<script[sS]+</script *>",
  188. RegexOptions.IgnoreCase);
  189. Regex regex2 =
  190. new Regex(@" href *= *[sS]*script *:",
  191. RegexOptions.IgnoreCase);
  192. Regex regex3 =
  193. new Regex(@" no[sS]*=",
  194. RegexOptions.IgnoreCase);
  195. Regex regex4 =
  196. new Regex(@"<iframe[sS]+</iframe *>",
  197. RegexOptions.IgnoreCase);
  198. Regex regex5 =
  199. new Regex(@"<frameset[sS]+</frameset *>",
  200. RegexOptions.IgnoreCase);
  201. Regex regex6 =
  202. new Regex(@"<img[^>]+>",
  203. RegexOptions.IgnoreCase);
  204. //Regex regex7 =
  205. // new Regex(@"</p>",
  206. // RegexOptions.IgnoreCase);
  207. //Regex regex8 =
  208. // new Regex(@"<p>",
  209. //RegexOptions.IgnoreCase);
  210. Regex regex9 =
  211. new Regex(@"<[^>]*>",
  212. RegexOptions.IgnoreCase);
  213. html = regex1.Replace(html, ""); //过滤<script></script>标记
  214. html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性
  215. html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
  216. html = regex4.Replace(html, ""); //过滤iframe
  217. html = regex5.Replace(html, ""); //过滤frameset
  218. html = regex6.Replace(html, ""); //过滤frameset
  219. html = regex9.Replace(html, "");
  220. html = Regex.Replace(html, "[\f\n\r\t\v]", ""); //过滤回车换行制表符
  221. int index = html.IndexOf("本文来源");//删除文本来源及责任编辑
  222. if (index != -1)
  223. {
  224. html = html.Substring(0, index - 1);
  225. }
  226. html = html.Replace("网易体育", "彩吧足球");
  227. return html;
  228. }
  229. #region 初始化信息
  230. /// <summary>
  231. /// 枚举类型
  232. /// </summary>
  233. private NewsTypeEnum currentNews => NewsTypeEnum.意甲;
  234. #endregion 初始化信息
  235. #region SQL语句
  236. /// <summary>
  237. ///查询类别对应的id
  238. /// </summary>
  239. private static string GetLotterySqlByTableName = @"SELECT TOP 1 [ID],[ItemId],[ItemName] FROM [dbo].[{0}] where ItemCode='NewsCategory' and [ItemName]='{1}' ";//WHERE [IsChecked] = 1 AND [IsPassed] = 1
  240. #endregion
  241. }
  242. }