天气小偷程序刚完成的一个天气小偷程序,从中央气象台自动采集信息入库
工程下载/************************************************/
using System.Text;
using ;
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 = "
/detail.php?city =" + 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"">)[^℃]*?(?=/|).*?(?=/)[/S]+(?=/).*?(?=/)^预.*(?=/)", ""); 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 + "
"); //Response.Write(maction[i+4].Value + "
"); 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+"
"); int val = go1000.SQLDBConnection.ExecuteNonQuery(CommandType.Text, strSql, parms); j += 4; n += 4; k += 4; m += 4; if (val > 0) Response.Write(""); else Response.Write(""); } //return; } else { Response.Write("发生错误"); } } }