/// <summary>
/// 国家气象局天气预报 3天
/// </summary>
/// <returns></returns>
public static XmlDataDocument GetCMAWeather()
{
//http://www.cma.gov.cn/tqyb/
//http://www.cma.gov.cn/tqyb/weatherdetail/57957.html
XmlDataDocument objXml = new XmlDataDocument();
objXml.LoadXml("<root />");
//抓取所有城市列表
string content = GetContent("http://www.cma.gov.cn/tqyb/", "gb2312");
//抓到内容后,开始分析数据
Regex regex;
Match mc;
XmlElement objXmlCityList = objXml.CreateElement("citylist");
string partten = "\"(?<citycode>[0-9]{5,})\"";
regex = new Regex(partten, RegexOptions.Compiled | RegexOptions.IgnoreCase);
objXmlCityList.SetAttribute("vdatetime", DateTime.Now.ToShortDateString());
Yesun.Edzh.BLL.Log.LogHelperService.WriteError("开始从中国气象局抓取天气预报");
//"50774"
int i = 1;
for (mc = regex.Match(content), i = 1; mc.Success; mc = mc.NextMatch(), i++)
{
try
{
//Yesun.Edzh.BLL.Log.LogHelperService.WriteError(i + "、" + mc.Groups["citycode"].Value.Trim());
//根据城市代码组合新的地址,抓取城市天气预报
if(Convert.ToInt32(mc.Groups["citycode"].Value) > 10000)
{
string cityUrl = "http://www.cma.gov.cn/tqyb/weatherdetail/" + mc.Groups["citycode"].Value + ".html";
//Yesun.Edzh.BLL.Log.LogHelperService.WriteError("CityUrl : " + cityUrl);
content = GetContent(cityUrl, "gb2312");
//真正开始分析天气预报数据
string parttenCity = "3天预报 (?<cityname>.*)</div>";
Match mcCity;
regex = new Regex(parttenCity, RegexOptions.Compiled | RegexOptions.IgnoreCase);
int j = 1;
for (mcCity = regex.Match(content), j = 1; mcCity.Success; mcCity = mcCity.NextMatch(), j++)
{
try
{
XmlElement objXmlElementCity = objXml.CreateElement("city");
Yesun.Edzh.BLL.Log.LogHelperService.WriteError("城市 : " + mcCity.Groups["cityname"].Value);
objXmlElementCity.SetAttribute("cityname", mcCity.Groups["cityname"].Value.Trim());
//读取天气信息
//日期
string parttenTmp = "<td width=\"(138|137)\" class=\"b-cn\">(?<item1>[^<]+)月(?<item2>[^<]+)日</td>";
Match mcTmp;
int k = 1;
regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)
{
if (k > 3)
break;
//Yesun.Edzh.BLL.Log.LogHelperService.WriteError("日期 : " + mcTmp.Groups["item1"].Value);
XmlElement itemElement = objXml.CreateElement("day");
itemElement.SetAttribute("vdatetime",DateTime.Now.Year+"-"+mcTmp.Groups["item1"].Value+"-"+mcTmp.Groups["item2"].Value);
objXmlElementCity.AppendChild(itemElement);
}
//天气
parttenTmp = "<td width=\"75\" valign=\"middle\" class=\"red-cn\">(?<item1>[^<]+)</td>";
k = 1;
regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)
{
//Yesun.Edzh.BLL.Log.LogHelperService.WriteError("天气 : " + mcTmp.Groups["item1"].Value);
//加入天气
XmlElement itemElement = (XmlElement)objXmlElementCity.SelectNodes("day")[k-1];
if (itemElement != null)
itemElement.SetAttribute("weather", mcTmp.Groups["item1"].Value);
}
//气温
parttenTmp = "class=\"b-cn\">(?<item1>[^<]+)℃</td>";
k = 1;
regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)
{
if (k > 3)
break;
//Yesun.Edzh.BLL.Log.LogHelperService.WriteError("气温 : " + mcTmp.Groups["item1"].Value);
//加入气温
XmlElement itemElement = (XmlElement)objXmlElementCity.SelectNodes("day")[k-1];
if (itemElement!=null)
itemElement.SetAttribute("temperature", mcTmp.Groups["item1"].Value + "℃");
}
//风力/风向
parttenTmp = "class=\"b-cn\">(?<item1>[^<]+)风</td>";
k = 1;
regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)
{
if (k > 3)
break;
//Yesun.Edzh.BLL.Log.LogHelperService.WriteError("风力/风向 : " + mcTmp.Groups["item1"].Value+"风");
//加入风力/风向
XmlElement itemElement = (XmlElement)objXmlElementCity.SelectNodes("day")[k-1];
if (itemElement != null)
itemElement.SetAttribute("wind", mcTmp.Groups["item1"].Value+"风");
}
//指数查询
parttenTmp = "<td width=\"67\" class=\"b-cn\">(?<item1>[^<]+)</td>(\\s*)<td valign=\"middle\" class=\"cn\"><a href=\"(?<xx>[^>]+)\" title=\"(?<item2>[^>]+)\">(?<title>[^<]+)</a></td>";
k = 1;
string comment = "";
regex = new Regex(parttenTmp, RegexOptions.Compiled | RegexOptions.IgnoreCase);
for (mcTmp = regex.Match(content), k = 1; mcTmp.Success; mcTmp = mcTmp.NextMatch(), k++)
{
//Yesun.Edzh.BLL.Log.LogHelperService.WriteError(mcTmp.Groups["item1"].Value +" : "+mcTmp.Groups["item2"].Value);
comment += mcTmp.Groups["item1"].Value + " : " + mcTmp.Groups["item2"].Value + "<br/>";
}
XmlElement objXmlComment = objXml.CreateElement("comment");
XmlCDataSection objCdata = objXml.CreateCDataSection(comment);
objXmlComment.AppendChild((XmlNode)objCdata);
objXmlElementCity.AppendChild(objXmlComment);
objXmlCityList.AppendChild(objXmlElementCity);
}
catch (Exception ex)
{
Yesun.Edzh.BLL.Log.LogHelperService.WriteError(ex.Message);
}
}
objXml.DocumentElement.AppendChild(objXmlCityList);
}
}
catch (Exception ex)
{
Yesun.Edzh.BLL.Log.LogHelperService.WriteError(ex.Message);
}
}
Yesun.Edzh.BLL.Log.LogHelperService.WriteError("成功从中国气象局抓取天气预报!");
return objXml;
}
/// <summary>
/// 抓取页面接口
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private static string GetContent(string url, string encoding)
{
string str = "";
WebClient client = new WebClient();
client.Headers.Add("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
client.Headers.Add("Accept-Language", "zh-cn");
client.Headers.Add("UA-CPU", "x86");
client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
try
{
byte[] buffer = client.DownloadData(url);
if (encoding == "utf-8")
{
str = System.Text.Encoding.GetEncoding("utf-8").GetString(buffer, 0, buffer.Length);
}
else
{
str = System.Text.Encoding.GetEncoding("gb2312").GetString(buffer, 0, buffer.Length);
}
}
catch (Exception ex)
{
Yesun.Edzh.BLL.Log.LogHelperService.WriteError(ex.Message);
}
return str;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端