前提:日常使用需要天气,每次都手写就很累。于是弄了个简单的winform,点一下按钮,自动获取,拼凑成字符串,然后可以粘贴。
1 private void button1_Click(object sender, EventArgs e) 2 { 3 var weatherCityCode = "101020100";//地区代码 4 5 string weatherInfoUrl = "http://wthrcdn.etouch.cn/WeatherApi?citykey=" + weatherCityCode; 6 string weatherstr = GetHtml(weatherInfoUrl); 7 resp tempInfo = XmlDeSeralizer<resp>(weatherstr); 8 9 weather wToDay = tempInfo.forecast[0]; 10 string low = wToDay.low.Replace("低温", "").Replace("℃", ""); 11 string high = wToDay.high.Replace("高温", ""); 12 string qing = string.Format("{0} {1} {2}", wToDay.day.type, wToDay.day.fengxiang, wToDay.day.fengli); 13 string temperStr = string.Format("{0}~{1} {2}", low, high, qing);//温度 14 15 #region 计算农历日期 16 ChineseLunisolarCalendar cncld = new ChineseLunisolarCalendar(); 17 DateTime dt = DateTime.Today; 18 int year = cncld.GetYear(dt); 19 // 是否有闰月,返回正整数(2020年闰4月,返回值为5) 20 int flag = cncld.GetLeapMonth(year); 21 int month = flag > 0 ? cncld.GetMonth(dt) - 1 : cncld.GetMonth(dt); 22 int day = cncld.GetDayOfMonth(dt); 23 //Console.WriteLine($"{dt:d},农历:{year}年{month}月{day}日"); 24 //农历日期 25 string[] DayName = {"*","初一","初二","初三","初四","初五", 26 "初六","初七","初八","初九","初十", 27 "十一","十二","十三","十四","十五", 28 "十六","十七","十八","十九","二十", 29 "廿一","廿二","廿三","廿四","廿五", 30 "廿六","廿七","廿八","廿九","三十","三十一"}; 31 string[] MonthName = { "*", "元", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" }; 32 string Nongli = string.Format("{0}月{1}", MonthName[month], DayName[day]);//农历 33 #endregion 34 35 string dateStr = string.Format("{0}.{1}", 36 System.DateTime.Today.Month, 37 System.DateTime.Today.Day);//公历日期 38 39 string weekStr = System.DateTime.Today.ToString("dddd", new System.Globalization.CultureInfo("zh-CN")); 40 41 string fullText = string.Format("{0} {1} {2} {3}", dateStr, Nongli, weekStr, temperStr);//公历,农历,星期,天气,温度 42 43 System.Windows.Forms.Clipboard.SetText(fullText); 44 45 this.Text = fullText; 46 } 47 48 private static string GetHtml(string url) 49 { 50 StringBuilder s = new StringBuilder(102400); 51 HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url); 52 wr.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate"; 53 HttpWebResponse response = (HttpWebResponse)wr.GetResponse(); 54 Head(response); 55 GZipStream g = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress); 56 byte[] d = new byte[20480]; 57 int l = g.Read(d, 0, 20480); 58 while (l > 0) 59 { 60 s.Append(Encoding.UTF8.GetString(d, 0, l)); 61 l = g.Read(d, 0, 20480); 62 } 63 return s.ToString(); 64 } 65 66 private static void Head(HttpWebResponse r) 67 { 68 string[] keys = r.Headers.AllKeys; 69 for (int i = 0; i < keys.Length; ++i) 70 { 71 Console.WriteLine(keys[i] + " " + r.Headers[keys[i]]); 72 } 73 } 74 75 public static T XmlDeSeralizer<T>(string xmlStr) where T : class, new() 76 { 77 XmlSerializer xs = new XmlSerializer(typeof(T)); 78 using (StringReader reader = new StringReader(xmlStr)) 79 { 80 return xs.Deserialize(reader) as T; 81 } 82 }
1 #region 天气抓取 2 /// <summary> 3 /// 代码转自 https://www.cnblogs.com/lgx5/p/10528845.html 4 /// </summary> 5 public class resp 6 { 7 public string city { get; set; } 8 public string updatetime { get; set; } 9 public string wendu { get; set; } 10 public string fengli { get; set; } 11 public string shidu { get; set; } 12 public string fengxiang { get; set; } 13 public environment environment { get; set; } 14 public alarm alarm { get; set; } 15 public List<weather> forecast { set; get; } 16 } 17 public class environment 18 { 19 public string aqi { get; set; } 20 public string pm25 { get; set; } 21 public string suggest { get; set; } 22 public string quality { get; set; } 23 public string MajorPollutants { get; set; } 24 public string time { get; set; } 25 } 26 public class alarm 27 { 28 public string cityName { get; set; } 29 public string alarmType { get; set; } 30 public string alarmDegree { get; set; } 31 public string alarmText { get; set; } 32 public string alarm_details { get; set; } 33 public string standard { get; set; } 34 public string suggest { get; set; } 35 } 36 public class weather 37 { 38 public string date { get; set; } 39 public string high { get; set; } 40 public string low { get; set; } 41 public climate day { get; set; } 42 public climate night { get; set; } 43 } 44 public class climate 45 { 46 public string type { get; set; } 47 public string fengxiang { get; set; } 48 public string fengli { get; set; } 49 } 50 #endregion
以上代码,亲测可行。