效果图
#region 获取IP地址
/// <summary>
/// 获取IP地址
/// </summary>
/// <returns></returns>
public static string GetIpAddress()
{
//获取IP地址
string IpString = StringHelper.GetHtmlContentByUrl("http://iframe.ip138.com/ic.asp", "gb2312");
string regcityString = @"\[(.*)\]";
string IpAddress = StringHelper.Reg(regcityString, IpString, 1);
return IpAddress;
}
#endregion
#region 获取当天星期农历
/// <summary>
/// 获取当天星期农历
/// </summary>
/// <returns></returns>
public static string GetDataString()
{
string dataString = StringHelper.GetHtmlContentByUrl("http://www.nongli114.com/", "gb2312");
//获取当天星期几
string regString = @"</b> ([^)]*?)</td>";
string data = StringHelper.Reg(regString, dataString, 1);
//获取当天农历
string regAString = @"<div id=""span_select_lunar_month"">([^)]*?)</div>";
string dataA = StringHelper.Reg(regAString, dataString, 1);
string regBString = @"<div id=""span_select_lunar_day"">([^)]*?)</div>";
string dataB = StringHelper.Reg(regBString, dataString, 1);
string dataForString = data+" "+"农历" + dataA + "月" + dataB + "日";
return dataForString;
}
#endregion
#region 获取地方天气
/// <summary>
/// 获取地方天气
/// </summary>
/// <returns></returns>
public static string GetWeatherString()
{
//获取地方天气
//string weatherAString = StringHelper.GetHtmlContentByUrl("http://61.4.185.48:81/g/","gb2312");
//string weatherReg = @"id=([^)]*?);if";
//string weatherID = StringHelper.Reg(weatherReg, weatherAString, 1);
string weatherID = "101200101";//获取武汉天气
string weatherBString = StringHelper.GetHtmlContentByUrl("http://m.weather.com.cn/data/" + weatherID + ".html", "utf-8");
string cityReg = @"""city"":""([^)]*?)""";
string cityString = StringHelper.Reg(cityReg, weatherBString, 1);
string weather1Reg = @"""weather1"":""([^)]*?)""";
string weather1String = StringHelper.Reg(weather1Reg, weatherBString, 1);
string temp1Reg = @"""temp1"":""([^)]*?)""";
string temp1String = StringHelper.Reg(temp1Reg, weatherBString, 1);
string weatherString = cityString + weather1String + temp1String;
return weatherString;
}
#endregion
补代码
#region 过滤匹配字符串 /// <summary> /// 过滤匹配字符串 /// </summary> /// <param name="regexstr">正则表达式</param> /// <param name="codestr">HTML/CODE/STRING</param> /// <returns></returns> public static string Reg(string regexstr, string codestr, int index) { Regex VIEWSTATERegex = new Regex(regexstr); MatchCollection VIEWSTATEMatchResult = VIEWSTATERegex.Matches(codestr); string regstr = ""; foreach (Match vmr in VIEWSTATEMatchResult) { regstr = vmr.Groups[index].Value.ToString(); } return regstr; } #endregion
/// <summary>
/// 根据url获取网站HTML内容 /// </summary> /// <param name="url">网址</param> /// <param name="url">编码</param> /// <returns>获取网站HTML内容</returns> public static string GetHtmlContentByUrl(string url, string encode) { var htmlContent = string.Empty; var httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.Timeout = 10000000; var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); var stream = httpWebResponse.GetResponseStream(); if (stream != null) { var streamReader = new StreamReader(stream, Encoding.GetEncoding(encode)); htmlContent = streamReader.ReadToEnd(); streamReader.Close(); streamReader.Dispose(); stream.Close(); stream.Dispose(); } httpWebResponse.Close(); return htmlContent; }