天气小偷程序
刚完成的一个天气小偷程序,从中央气象台自动采集信息入库
工程下载/************************************************/ using System.Text; using System.Net; using System.IO; using System.Text.RegularExpressions; using MSXML2; using System.Xml; public partial class userControls_weather : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string id = DropDownList1.SelectedValue; string city = DropDownList1.SelectedItem.Text; /***********************直接从雅虎网站偷取数据*************************************************************/ string Url = " http://weather.tq121.com.cn/detail.php?city /**************************************************************** WebRequest myWebRequest = WebRequest.Create(Url); WebResponse myWebResponse = myWebRequest.GetResponse(); System.IO.Stream stream = myWebResponse.GetResponseStream(); StreamReader sr = new StreamReader(stream, Encoding.Default); //以字符串形式读取数据流 string respstr = sr.ReadToEnd(); sr.Close(); Response.Write(respstr); ****************************************************************/ MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass(); _xmlhttp.open("GET", Url, true, null, null); _xmlhttp.setRequestHeader("Content-Type", "text/xml;charset=gb2312"); _xmlhttp.send(null); if (_xmlhttp.readyState == 4) { //匹配日期,最高温,最低温 string patter = @"(?<=span class=""big-cn"">)[^℃]*?(?=/</span)|(?<=img src="").*?[a|b]{1}[0-9]{1,2}.gif(?=/"")|(?<=<span class=""big-cn"">|<span class=""wendu"">).*?(?=/</span)|(?<=span class=""cn"">)[/S]+(?=/</span)"; //匹配日期,最高温,最低温,天气图标 //string patter = @"(?<=th>).*?(?=/</th)|(?<=gr/).*?png(?=/))|(?<=High:).*?(?=/</strong)|(?<=Low:).*?(?=/</span)"; string str = Encoding.Default.GetString((byte[])_xmlhttp.responseBody); //Response.Write(str); return; str = Regex.Replace(str, @"(?<=span class=""big-cn"">)^预.*(?=/</span>)", ""); MatchCollection maction = Regex.Matches(str, patter); int j = 1; int n = 2; int k = 4; int m = 5; for (int i = 0; i < 5; i++) { //Response.Write(maction[i+1].Value + "<br />"); //Response.Write(maction[i+4].Value + "<br />"); string strSql = "insert into go_weather(submit_date,weather_city,weather_date,weather_img,weather_minTemp,weather_maxTemp,weather_wind) values(@subTime,@city,@date,@img,@min,@max,@wind)"; //string strSql = "insert into go_weather(submit_date,weather_city,weather_date,weather_minTemp,weather_maxTemp,weather_wind) values(" + DateTime.Today + "," + city + "," + maction[i + j].Value + "," + maction[i + k].Value + "," + maction[i + k].Value + "," + maction[i + m].Value + ")"; SqlParameter[] parms = { new SqlParameter("@subTime",SqlDbType.VarChar,16), new SqlParameter("@city",SqlDbType.VarChar,20), new SqlParameter("@date",SqlDbType.VarChar,20), new SqlParameter("@img",SqlDbType.VarChar,30), new SqlParameter("@min",SqlDbType.VarChar,20), new SqlParameter("@max",SqlDbType.VarChar,20), new SqlParameter("@wind",SqlDbType.VarChar,50)}; parms[0].Value = DateTime.Today; parms[1].Value = city; parms[2].Value = maction[i + j].Value; parms[3].Value = maction[i + n].Value.Replace("../images","../images/weather"); parms[4].Value = maction[i + k].Value; parms[5].Value = maction[i + k].Value; parms[6].Value = maction[i + m].Value; Response.Write(strSql+"<br />"); int val = go1000.SQLDBConnection.ExecuteNonQuery(CommandType.Text, strSql, parms); j += 4; n += 4; k += 4; m += 4; if (val > 0) Response.Write("<script>alert('aok;);</script>"); else Response.Write("<script>alert('error;);</script>"); } //return; } else { Response.Write("发生错误"); } } } |
天气小偷程序,自动采集信息入库
最新推荐文章于 2021-03-19 11:05:21 发布