文字天気予報

19375 ワード

近日は仕事の都合で、ページに1行表示の天気予報を挿入する必要があります.のネット上でN久を探して、もしかすると方法が得られないためかも知れなくて、心のうちに、手を出すしかなくて、今出したものをここに送って、必要な友达に役に立つことができることを望んであるいは啓発します.
 
(コードはC#,ASP.net 2.0の環境で、乱れていますが、大体読めますよね....)
 
ASPXファイルをtqybという名前で作成します.aspx、そのうち.aspxファイルの内容は気にする必要はありません.aspx.csの内容は以下の通りです.
 using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

public partial class _tqyb : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //-- , www.cma.gov.cn
        string pageUrl = "http://www.cma.gov.cn/tqyb/weatherdetail/57496.html";

        //--
        string dd = string.Empty;

        //--
        string tqgk = string.Empty;

        //--
        string qw = string.Empty;

        //-- /
        string fxfl = string.Empty;

        if (!IsPostBack)
        {
            WebClient wc = new WebClient();
            wc.Credentials = CredentialCache.DefaultCredentials;

            Byte[] bt = wc.DownloadData(@pageUrl);

            string result = Encoding.Default.GetString(bt);

            if (result.Length <= 0)
            {
                Response.Write(" !");
                return;
            }
            
            int pos = result.IndexOf("<div class=\"font01\">3 &nbsp;&nbsp;&nbsp;</div>");

            if (pos > 0)
                result = " !";
            else
            {
                pos = result.IndexOf("<div class=\"font01\">3 ");

                result = result.Substring(pos, result.IndexOf(" ") - pos);

                pos = result.IndexOf("&nbsp;&nbsp;&nbsp;") + 18;
                dd = result.Substring(pos, result.IndexOf("</") - pos);

                pos = result.IndexOf(" ");
                result = result.Substring(pos, result.IndexOf("<!--<tr>") - pos);

                pos = result.IndexOf("<td width=\"75\"");
                result = result.Substring(pos);

                pos = result.IndexOf(">");
                tqgk = result.Substring(pos + 1, result.IndexOf("</td>") - pos - 1);

                result = result.Substring(result.IndexOf(" "));
                pos = result.IndexOf("cn\">") + 4;

                qw = result.Substring(pos, result.IndexOf("℃</td>") - pos + 1);

                result = result.Substring(result.IndexOf(" / </td>"));
                result = result.Substring(result.IndexOf("<td"));

                pos = result.IndexOf("cn\">") + 4;

                fxfl = result.Substring(pos, result.IndexOf("</td") - pos);

                tqgk = " :" + tqgk.Replace(" ", "");
                qw = qw.Replace(" ", "");

                qw = " :" + qw.Replace("/", " :");

                fxfl = " / :" + fxfl.Replace(" ", "");

                result = dd + tqgk + " " + qw + " " + fxfl;

            }

            Response.Write("document.write(\"" + result + "\");");
            Response.End();
            
        }
    }
}

次のように呼び出すだけです.<script language="javascript" type="text/javascript" src="tqyb.aspx"></script>
純文字の当日天気予報が得られ、自分でどんなフォーマットを設定してもよい.