修改其中的url获得其他城市的天气情况
如广州为:
http://weather.yahoo.com/forecast/chxx0037_c.html
注意仅适用于获得yahoo上的天气预报
以下为引用的内容: getweather.aspx ----------------------------------- <%@ page language="c#" codebehind="getweather.aspx.cs" autoeventwireup="false" inherits="test.getweather" %> <!doctype html public "-//w3c//dtd html 4.0 transitional//en" > <html> <head> <title>getweather</title> <meta name="generator" content="microsoft visual studio 7.0"> <meta name="code_language" content="c#"> <meta name="vs_defaultclientscript" content="javascript"> <meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5"> </head> <body> <form id="getweather" method="post" runat="server"> <font face="宋体"> <p> <asp:label id="lblweather" runat="server">weather</asp:label></p> <p> <asp:button id="btnget" runat="server" text="get weather"></asp:button></p> <p> <asp:label id="weather2" runat="server">24小时天气</asp:label></p> <p> <asp:button id="btnget2" runat="server" text="天气预报"></asp:button></p> </font> </form> </body> </html> getweather.aspx.cs --------------------------------------- using system; using system.collections; using system.componentmodel; using system.data; using system.drawing; using system.web; using system.web.sessionstate; using system.web.ui; using system.web.ui.webcontrols; using system.web.ui.htmlcontrols; using system.net; using system.io; namespace test { public class getweather : system.web.ui.page { protected system.web.ui.webcontrols.label lblweather; protected system.web.ui.webcontrols.label weather2; protected system.web.ui.webcontrols.button btnget2; protected system.web.ui.webcontrols.button btnget; private void page_load(object sender, system.eventargs e) { // put user code to initialize the page here } #region web form designer generated code override protected void oninit(eventargs e) { // // codegen: this call is required by the asp.net web form designer. // initializecomponent(); base.oninit(e); } /// <summary> /// required method for designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void initializecomponent() { this.btnget.click += new system.eventhandler(this.btnget_click); this.btnget2.click += new system.eventhandler(this.btnget2_click); this.load += new system.eventhandler(this.page_load); } #endregion private void btnget_click(object sender, system.eventargs e) { webrequest wreq=webrequest.create("http://weather.yahoo.com/forecast/chxx0037_c.html"); httpwebresponse wresp=(httpwebresponse)wreq.getresponse(); string html =""; stream s=wresp.getresponsestream(); streamreader objreader = new streamreader(s); string sline = ""; int i = 0; while (sline!=null) { i++; sline = objreader.readline(); if (sline!=null) html += sline; } string temp= ""; int start,stop; start = html.indexof("<!-- curcon-->",0,html.length); stop = html.indexof("<!-- end curcon-->",0,html.length); temp = html.substring(start, stop - start); start = temp.indexof("<b>"); stop = temp.indexof("</b>"); string degree = temp.substring(start+3,stop - start -3); start = temp.indexof("<img"); stop = temp.indexof("</td>",start); string img = temp.substring(start,stop - start); lblweather.text = degree + "<br>" + img; } private void btnget2_click(object sender, system.eventargs e) { webrequest wreq=webrequest.create("http://cn.weather.yahoo.com/chxx/chxx0037/index_c.html"); httpwebresponse wresp=(httpwebresponse)wreq.getresponse(); string html =""; stream s=wresp.getresponsestream(); streamreader objreader = new streamreader(s,system.text.encoding.getencoding("gb2312")); string sline = ""; int i = 0; while (sline!=null) { i++; sline = objreader.readline(); if (sline!=null) html += sline; } string temp= ""; int start,stop; start = html.indexof("<table border=0 cellpadding=2 cellspacing=1 bgcolor=9999cc width=/"85%/">",0,html.length); stop = html.indexof("</table>",start)+8; temp = html.substring(start, stop - start); weather2.text = temp; } } } |
新闻热点
疑难解答
图片精选