根据城市和日期获取天气信息,未来的天气预报建议获取7天内的比较准
public List<WeatherRes> GetWeath( DateTime date, string address)
{
var ReqUrl = "http://d1.weather.com.cn/calendar_new/";
var HostUrl = "http://www.weather.com.cn/";
var iUrl = ReqUrl + date.GDateString("yyyy") + "/" + address + "_" + date.GDateString("yyyyMM") + ".html";
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("_", DateTimeHelper.GetLong(DateTime.Now, 10000).GString());
var apiRes = GET(iUrl, dict, null, HostUrl);
var dyic = apiRes.IndexOf("[");
var zhyic = apiRes.LastIndexOf("]");
apiRes = apiRes.Substring(dyic, zhyic - dyic + 1);
var result = apiRes.ToObject<List<HistoryRes>>();
var res = result.Select(p => new WeatherRes()
{
date = DateTime.ParseExact(p.date, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture).GDateString("yyyy-MM-dd"),
max = string.IsNullOrEmpty(p.max) ? p.maxobs : p.max,
maxobs = string.IsNullOrEmpty(p.maxobs) ? p.max : p.maxobs,
min = string.IsNullOrEmpty(p.min) ? p.minobs : p.min,
minobs = string.IsNullOrEmpty(p.minobs) ? p.min : p.minobs,
rainobs = p.rainobs,
tq = p.w1,
fd = p.wd1
}).ToList();
res = res.Where(p => p.date.GDateTime() > DateTime.Now.AddDays(-6)).ToList();
return res;
}
public static string GET(string url, Dictionary<string, string> param = null, NameValueCollection heder = null, string referer = "")
{
StringBuilder postData = new StringBuilder();
if (param != null && param.Count > 0)
{
foreach (var p in param)
{
if (postData.Length > 0)
{
postData.Append("&");
}
postData.Append(p.Key);
postData.Append("=");
postData.Append(p.Value);
}
}
var postDataStr = postData.ToString();
return GET(url, postDataStr, heder, referer);
}
public static string GET(string url, string queryString, NameValueCollection heder = null,string referer="")
{
var result = "";
if (url.IndexOf("?") < 0)
{
url += "?";
}
if (url.IndexOf("=") > 0)
{
url += "&";
}
if (!string.IsNullOrEmpty(queryString))
{
url += queryString;
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//添加参数
if (heder != null)
{
request.Headers.Add(heder);
}
if(!string.IsNullOrEmpty(referer))
{
request.Referer = referer;
}
HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
Stream stream = resp.GetResponseStream();
try
{
//获取内容
using (StreamReader reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
}
finally
{
stream.Close();
}
return result;
}
/// 返回实体 根据自己的需求定义
public class WeatherRes
{
/// <summary>
/// 日期 2022-08-31
/// </summary>
public string date { get; set; }
/// <summary>
/// 预计最高气温
/// </summary>
public string max { get; set; }
/// <summary>
/// 实际最高气温
/// </summary>
public string maxobs { get; set; }
/// <summary>
/// 预计最低气温
/// </summary>
public string min { get; set; }
/// <summary>
/// 实际最低气温
/// </summary>
public string minobs { get; set; }
/// <summary>
/// 降雨量 单位 mm
/// </summary>
public string rainobs { get; set; }
/// <summary>
/// 天气 多云
/// </summary>
public string tq { get; set; }
/// <summary>
/// 风 微风
/// </summary>
public string fd { get; set; }
}