C#使用WebClient爬取天气数据
天气数据地址:https://m.tianqi.com/lishi/
传入参数说明
citypy:城市拼音
historyurl:爬取地址https://m.tianqi.com/lishi/
month:月份,如202112
代码实现
爬取前一天数据
string month = DateTime.Today.AddDays(-1).ToString("yyyyMM");
void getdata(string citypy,string historyurl,string month){
string url=historyurl+citypy+"/index.html";
string strHTML = null;
using (WebClient myWebClient = new WebClient { Credentials = CredentialCache.DefaultCredentials }){
Stream myStream = myWebClient.OpenRead(url);
using (StreamReader sr = new StreamReader(myStream, Encoding.UTF8)){
strHTML = sr.ReadToEnd();
}
}
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(strHTML);
var res = doc.DocumentNode.SelectNodes(@"/html[1]/body[1]/div[11]/a");
if (res != null){
int index = res.Count - 2;//减去天气预报的行
webfiled wf = new webfiled();//天气对象
var nodes = res[index].SelectNodes(@"div");
var date = nodes[0].InnerText.Replace(" ", "").Replace("-", "/");
wf.dates1 = Convert.ToDateTime(date.Substring(0, 10));//日期
wf.week = date.Substring(10);//周几
wf.high_1 = nodes[1].InnerText.Replace("℃", "");//最高气温
wf.low_1 = nodes[2].InnerText.Replace("℃", "");//最低气温
wf.type_1 = nodes[3].InnerText.Trim();//天气
wf.fx_1 = nodes[4].InnerText.Trim();//风向
//可以继续写入数据到数据库
}
}
爬取一个月的天气数据
var res = doc.DocumentNode.SelectNodes(@"/html[1]/body[1]/div[10]/a");
if (res != null)
{
webfiled wf = new webfiled();
for(int aNum = 0; aNum < res.Count; aNum++)
{
var nodes= res[aNum].SelectNodes(@"div");
var date = nodes[0].InnerText.Replace(" ", "").Replace("-", "/");
wf.dates1 = Convert.ToDateTime(date.Substring(0,10));
if (wf.dates1 > DateTime.Today)//如果这是当前月,需要除去今天以后的天气预报,
break;
wf.week = date.Substring(10);
wf.high_1 = nodes[1].InnerText.Replace("℃","");
wf.low_1= nodes[2].InnerText.Replace("℃", "");
wf.type_1 = nodes[3].InnerText.Trim();
wf.fx_1 = nodes[4].InnerText.Trim();
//可以继续写如数据
}
}