/// <summary> /// 处理币html数据 /// </summary> /// <param name="htmlDetail"></param> /// <param name="sourceId"></param> public static bool HandleCoinHtml(string coin, string htmlDetail, ref List <BitcoinEntity> resultList, ref List <CoinResultEntity> coinResultList, ref string msg , int count = 5, decimal defaultPrecent = (decimal)0.1) { htmlDetail = CommonTools.Compress(htmlDetail); List <BitcoinEntity> list = new List <BitcoinEntity>(); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(htmlDetail); List <string> platFormList = ConfigHelper.GetConfigString("PlatForms").ToLower().ToStringList(); HtmlNode tableNode = doc.DocumentNode.SelectSingleNode("//table[@id='markets']//tbody"); if (tableNode == null) { msg = "获取不到数据"; return(false); } Regex trRegex = new Regex(@"<tr[\s\S]*?添加自选</div>", RegexOptions.IgnoreCase); Regex tdRegex = new Regex("<tr><td>[\\d\\*]+<td>([\\s\\S]+?)<td>([^<]+?)<td class=\"price\"[^>]+?data-btc=\"([\\d\\.]+)\"[^>]+?>([^<]+?)<td>([^<]+?)<td class=\"volume\"[^>]+?>([^<]+?)<td>([^<]+?)<td>([\\d\u4e00-\u9fa5]+?)<td>", RegexOptions.IgnoreCase); Regex platRegex = new Regex(@"<img[^>]+?>([^>]+?)</a>", RegexOptions.IgnoreCase); MatchCollection matchs = trRegex.Matches(tableNode.InnerHtml); int i = 0; foreach (Match item in matchs) { string trDetail = item.Value; Match tdmatchs = tdRegex.Match(trDetail); if (tdmatchs.Success) { i++; BitcoinEntity coinEnt = new BitcoinEntity(); coinEnt.Id = i + 1; coinEnt.Name = coin; coinEnt.PlatFormHtml = tdmatchs.Groups[1].Value.Replace("/exchange", "https://www.feixiaohao.com/exchange"); Match platMatch = platRegex.Match(coinEnt.PlatFormHtml); if (platMatch.Success) { coinEnt.PlatForm = platMatch.Groups[1].Value; } coinEnt.ExchangeType = tdmatchs.Groups[2].Value; string aPrice = tdmatchs.Groups[4].Value; decimal price = 0; decimal btcPrice = 0; if ((aPrice.Contains("¥"))) { price = Convert.ToDecimal(aPrice.Replace("¥", "").Replace(",", "")); btcPrice = CommonTools.ChangeDataToD(tdmatchs.Groups[3].Value); } if (price == 0) { msg = "价格为0"; continue; } coinEnt.Amout = tdmatchs.Groups[5].Value; coinEnt.TotalPrice = tdmatchs.Groups[6].Value; coinEnt.Percent = Convert.ToDecimal(tdmatchs.Groups[7].Value.Replace("%", "")); coinEnt.Time = tdmatchs.Groups[8].Value; coinEnt.Price = price; coinEnt.BtcPrice = btcPrice; if (platFormList.Contains(coinEnt.PlatForm)) { continue; } //该平台已存在 if (list.Exists(x => x.PlatForm == coinEnt.PlatForm)) { continue; } list.Add(coinEnt); } } //if (trNodeList.Count < 2) //{ // msg = "数据量不足"; // return false; //} //for (int i = 0; i < trNodeList.Count; i++) //{ // BitcoinEntity coinEnt = new BitcoinEntity(); // HtmlNode trNode= HtmlNode.CreateNode(trNodeList[i].OuterHtml); // HtmlNodeCollection tdNodeList = trNode.SelectNodes("./td"); // if (tdNodeList == null || tdNodeList.Count < 9) // { // msg = "html结构有变化"; // return false; // } // decimal price =0; // decimal btcPrice =0; // if ((tdNodeList[3].InnerText.Contains("¥"))) // { // price = Convert.ToDecimal(tdNodeList[3].InnerText.Replace("¥", "").Replace(",", "")); // HtmlAttributeCollection attrColl = tdNodeList[3].Attributes; // btcPrice = CommonTools.ChangeDataToD(attrColl["data-btc"].Value); // } // if (price==0) // { // msg = "价格为0"; // continue; // } // coinEnt.Id = i+1; // coinEnt.Name = coin; // coinEnt.PlatForm = tdNodeList[1].InnerText.ToLower().Trim(); // coinEnt.PlatFormHtml = tdNodeList[1].InnerHtml.Replace("/exchange", "https://www.feixiaohao.com/exchange"); // coinEnt.ExchangeType = tdNodeList[2].InnerHtml.Replace("/exchange", "https://www.feixiaohao.com/exchange"); // coinEnt.Price = price; // coinEnt.BtcPrice = btcPrice; // coinEnt.Amout = tdNodeList[4].InnerText; // coinEnt.TotalPrice = tdNodeList[5].InnerText; // coinEnt.Percent = Convert.ToDecimal(tdNodeList[6].InnerText.Replace("%", "")); // coinEnt.Time = tdNodeList[7].InnerText; // if (platFormList.Contains(coinEnt.PlatForm)) // { // continue; // } // //该平台已存在 // if (list.Exists(x=>x.PlatForm == coinEnt.PlatForm)) // { // continue; // } // list.Add(coinEnt); //} if (list.Count < 2) { msg = "数据量不足"; return(false); } //先按占比排序取前n个,然后按价格排序 list = list.OrderByDescending(x => x.Percent).Take(count).ToList().OrderByDescending(x => x.BtcPrice).ToList(); BitcoinEntity firstEnt = list.First(); BitcoinEntity lastEnt = list.Last(); decimal margin = firstEnt.BtcPrice - lastEnt.BtcPrice; if (firstEnt.BtcPrice > 0) { decimal percent = margin / firstEnt.BtcPrice; if (percent >= defaultPrecent) { resultList = list; coinResultList.Add(new CoinResultEntity() { Name = coin, Margin = CommonTools.ChangeDataToD(margin.ToString()), Proportion = Convert.ToDecimal((percent * 100).ToString("f2")) }); return(true); } } return(false); }
/// <summary> /// 处理平台html数据 /// </summary> /// <param name="htmlDetail"></param> /// <param name="sourceId"></param> public static bool HandlePlatFornHtml(string platForm, string htmlDetail, ref List <BitcoinEntity> resultList, ref string msg) { htmlDetail = CommonTools.Compress(htmlDetail); //HtmlDocument doc = new HtmlDocument(); //doc.LoadHtml(htmlDetail); // LogHelper.LogInfo(htmlDetail); Regex tableRegex = new Regex("<table class=\"table noBg\">[\\s\\S]+?</table>", RegexOptions.IgnoreCase); Regex trRegex = new Regex(@"<tr[\s\S]+?添加自选</div>", RegexOptions.IgnoreCase); Regex tdRegex = new Regex(@"<tr><td>[\d\*]+<td>([\s\S]+?)<td>([^<]+?)<td[^>]+?data-btc=([\d\.]+)[^>]+?>([^<]+?)<td>([^<]+?)<td[^>]+?>([^<]+?)<td>([^<]+?)<td>([\d\u4e00-\u9fa5]+?)<td>", RegexOptions.IgnoreCase); Regex platRegex = new Regex(@"<img[^>]+?>([^>]+?)</a>", RegexOptions.IgnoreCase); Match tablematchs = tableRegex.Match(htmlDetail); if (!tablematchs.Success) { msg = "获取不到数据"; return(false); } //HtmlNode tableNode = doc.DocumentNode.SelectSingleNode("//table[@class='table noBg']//tbody"); // LogHelper.LogInfo(tablematchs.Value); //tableNode.InnerHtml = ""; MatchCollection matchs = trRegex.Matches(tablematchs.Value); int i = 0; foreach (Match item in matchs) { string trDetail = item.Value; Match tdmatchs = tdRegex.Match(trDetail); if (tdmatchs.Success) { i++; BitcoinEntity coinEnt = new BitcoinEntity(); coinEnt.Id = i + 1; coinEnt.PlatForm = platForm; coinEnt.NameHtml = tdmatchs.Groups[1].Value.Replace("/exchange", "https://www.feixiaohao.com/exchange"); Match platMatch = platRegex.Match(coinEnt.NameHtml); if (platMatch.Success) { coinEnt.Name = platMatch.Groups[1].Value; } coinEnt.ExchangeType = tdmatchs.Groups[2].Value; string aPrice = tdmatchs.Groups[4].Value; decimal price = 0; decimal btcPrice = 0; if ((aPrice.Contains("¥"))) { price = Convert.ToDecimal(aPrice.Replace("¥", "").Replace(",", "")); btcPrice = CommonTools.ChangeDataToD(tdmatchs.Groups[3].Value); } if (price == 0) { msg = "价格为0"; continue; } coinEnt.Amout = tdmatchs.Groups[5].Value; coinEnt.TotalPrice = tdmatchs.Groups[6].Value; coinEnt.Percent = Convert.ToDecimal(tdmatchs.Groups[7].Value.Replace("%", "")); coinEnt.Time = tdmatchs.Groups[8].Value; coinEnt.Price = price; coinEnt.BtcPrice = btcPrice; string nameEnglish = ""; string[] arr = coinEnt.ExchangeType.Split('/'); if (arr.Length == 2) { nameEnglish = arr[0].ToUpper(); } coinEnt.NameEnglish = nameEnglish; if (resultList.Exists(x => x.NameEnglish.ToLower() == coinEnt.NameEnglish.ToLower())) { continue; } resultList.Add(coinEnt); } } //for (int i = 0; i < trNodeList.Count; i++) //{ // BitcoinEntity coinEnt = new BitcoinEntity(); // HtmlNodeCollection tdNodeList = trNodeList[i].ChildNodes; // if (tdNodeList == null || tdNodeList.Count < 9) // { // msg = "html结构有变化"; // return false; // } // decimal price = 0; // decimal btcPrice = 0; // if ((tdNodeList[3].InnerText.Contains("¥"))) // { // price = Convert.ToDecimal(tdNodeList[3].InnerText.Replace("¥", "").Replace(",", "")); // HtmlAttributeCollection attrColl = tdNodeList[3].Attributes; // btcPrice = CommonTools.ChangeDataToD(attrColl["data-btc"].Value); // } // string name = tdNodeList[1].InnerText.ToLower().Trim(); // coinEnt.Id = i + 1; // coinEnt.PlatForm = platForm; // coinEnt.Name = name; // coinEnt.NameHtml = tdNodeList[1].InnerHtml.Replace("/currencies", "https://www.feixiaohao.com/currencies"); // coinEnt.ExchangeType = tdNodeList[2].InnerText.Trim(); // coinEnt.Price = price; // coinEnt.BtcPrice = btcPrice; // coinEnt.Amout = tdNodeList[4].InnerText; // coinEnt.TotalPrice = tdNodeList[5].InnerText; // coinEnt.Percent = Convert.ToDecimal(tdNodeList[6].InnerText.Replace("%", "")); // coinEnt.Time = tdNodeList[7].InnerText; // string nameEnglish = ""; // string[] arr = coinEnt.ExchangeType.Split('/'); // if (arr.Length == 2) // { // nameEnglish = arr[0].ToUpper(); // } // coinEnt.NameEnglish = nameEnglish; // //if (!coinEnt.ExchangeType.ToUpper().Contains("BTC")) // //{ // // continue; // //} // if (resultList.Exists(x => x.NameEnglish.ToLower() == coinEnt.NameEnglish.ToLower())) // { // continue; // } // resultList.Add(coinEnt); //} return(true); }