[C#] 透過 Regex 取得星座資料,純分享勿戰

2018-07-10

最近在看一些東西,要參考一些星座上的資料,網路上看到這一篇文件 : https://my.oschina.net/cart/blog/191021 ,這裡面有簡單教學一下怎麼去 parse Sina 網站上的星座資料,我就是稍微改寫一下變成自己需要的,因為我是配合我自己要的方式去開物件,如果你不喜歡或是覺得我寫的效能很差就自行改寫吧,我是設計拿到這樣的結果。

{  
   "StarSign":{  
      "Title":"雙子座",
      "Id":"gemini",
      "DateRange":"05/21-06/21"
   },
   "Date":"2018-07-10",
   "Desc":"今天是你的感情红心日,能够在基于理智的基础上处理好感情问题,不管是有对象的人还是单身党,都能够在感情方面有不错的进展和收获。不过今天在与人沟通的时候,要注意管好自己的嘴,不要因为一个不小心说漏了嘴而对别人不利。",
   "Datas":{  
      "健康指数":"70%",
      "幸运颜色":"褐色",
      "幸运数字":"7",
      "速配星座":"天秤座",
      "今日提醒":"注意沟通",
      "去做":"管好自己的嘴",
      "别做":"说漏嘴",
      "月亮能量":"月亮落金牛座29.25°-双子座13.54°(此处是指每日月亮行动轨迹)"
   }
}


這是我的程式碼 C# code :

using System.Linq; public static class StarSignsUtil { /// <summary> /// 紀錄星座資料的模型 /// </summary> public class StarSignInfo { public string Title { get; set; } public string Id { get; set; } public string DateRange { get; set; } } /// <summary> /// 查詢後的回應資料 /// </summary> public class FateResult { public StarSignInfo StarSign { get; set; } public string Date { get; set; } public string Desc { get; set; } public System.Collections.Generic.Dictionary<string, string> Datas { get; set; } public FateResult() { Datas = new System.Collections.Generic.Dictionary<string, string>(); } } /// <summary> /// 星座資料 /// </summary> public static System.Collections.Generic.List<StarSignInfo> Datas { get; set; } //Ctor static StarSignsUtil() { Datas = new System.Collections.Generic.List<StarSignInfo>(); Datas.Add(new StarSignInfo { Title = "白羊座", Id = "aries", DateRange = "03/21-04/19" }); Datas.Add(new StarSignInfo { Title = "金牛座", Id = "taurus", DateRange = "04/20-05/20" }); Datas.Add(new StarSignInfo { Title = "雙子座", Id = "gemini", DateRange = "05/21-06/21" }); Datas.Add(new StarSignInfo { Title = "巨蟹座", Id = "cancer", DateRange = "06/22-07/22" }); Datas.Add(new StarSignInfo { Title = "獅子座", Id = "leo", DateRange = "07/23-08/22" }); Datas.Add(new StarSignInfo { Title = "處女座", Id = "virgo", DateRange = "08/23-09/22" }); Datas.Add(new StarSignInfo { Title = "天秤座", Id = "libra", DateRange = "09/23-10/23" }); Datas.Add(new StarSignInfo { Title = "天蠍座", Id = "scorpio", DateRange = "10/24-11/22" }); Datas.Add(new StarSignInfo { Title = "射手座", Id = "sagittarius", DateRange = "11/23-12/21" }); Datas.Add(new StarSignInfo { Title = "魔羯座", Id = "capricorn", DateRange = "12/22-01/19" }); Datas.Add(new StarSignInfo { Title = "水瓶座", Id = "aquarius", DateRange = "01/20-02/18" }); Datas.Add(new StarSignInfo { Title = "雙魚座", Id = "pisces", DateRange = "02/19-03/20" }); } public static FateResult GetFateToday(string title) { var sInfo = Datas.SingleOrDefault(x => x.Title == title); if (sInfo == null) { throw new System.Exception("此星座我沒有找到"); } var result = new StarSignsUtil.FateResult(); result.StarSign = sInfo; var wc = new System.Net.WebClient(); wc.Encoding = System.Text.Encoding.UTF8; var source = wc.DownloadString("http://vip.astro.sina.com.cn/astro/view/" + sInfo.Id + "/"); var regex = new System.Text.RegularExpressions.Regex(@"<div class=[\s""']tab[\s""']><h4>(?<KEY>.*?)</h4><p>(?<VALUE>.*?)</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.MatchCollection matches = regex.Matches(source); foreach (System.Text.RegularExpressions.Match match in matches) { if (match.Success) { var k = match.Groups["KEY"].Value.Replace("&nbsp;", ""); var v = match.Groups["VALUE"].Value.Replace("&nbsp;", ""); if (!string.IsNullOrEmpty(k) && !string.IsNullOrEmpty(v) && !result.Datas.ContainsKey(k)) { result.Datas.Add(k, v); } } } regex = new System.Text.RegularExpressions.Regex(@"&lt;p&gt;(?<VALUE>.*?)&lt;/p&gt", System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline); matches = regex.Matches(source); foreach (System.Text.RegularExpressions.Match match in matches) { if (match.Success) { var v = match.Groups["VALUE"].Value.Trim(); if (!string.IsNullOrEmpty(v)) { if (v.Contains("&#65306;")) { result.Datas.Add(v.Split('&#65306;')[0].Trim(), v.Split('&#65306;')[1].Trim()); } else { result.Desc = v.Trim(); } } } } regex = new System.Text.RegularExpressions.Regex(@"有效日期:(?<VALUE>.*?)</li>", System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline); matches = regex.Matches(source); foreach (System.Text.RegularExpressions.Match match in matches) { if (match.Success) { result.Date = match.Groups["VALUE"].Value.Trim(); } } return result; } }

呼叫方式也很簡單只要這樣 :

  var genimi = StarSignsUtil.GetFateToday("雙子座");


基本上自己做一下 cache 不要一直取,不然我相信很快就不能用了,基本上,能用多久我也不知道。

就自行取用吧 :)


當麻許的碎念筆記 2014 | Donma Hsu Design.