zoukankan      html  css  js  c++  java
  • 基于百度搜索开放平台的天气查询

    思路: 利用百度搜索开放平台,简单分析百度搜索URL参数,请求查询页面,再利用正则表达式,提取相关内容实现。

    效果如下: 


    protected void On_Submit(object sender, EventArgs e)
        {
            
    string city = Request.Params["txtcity"].ToString();
            
    string tqyb = "天气预报";
            tqyb = HttpUtility.UrlEncode(tqyb, System.Text.Encoding.GetEncoding("gb2312"));
            city = HttpUtility.UrlEncode(city, System.Text.Encoding.GetEncoding("gb2312"));
            
    string url = string.Format("http://www.baidu.com/s?bs={0}+{1}f=8&wd={0}+{1}", tqyb, city);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
            
    string html = reader.ReadToEnd();
            
    string pattern = "<\\s*TABLE cellspacing=\"0\" cellpadding=\"0\" class=\"al_wt\">([\\s\\S]*?)<\\s*\\/TABLE\\s*>";
            Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
            MatchCollection item = reg.Matches(html);
            
    foreach (Match m in item)
            {
                html = m.Value;
            }
            Div_html.InnerHtml = html;
        }

     当city为空时,查询是你所在城市的天气,你所在城市计算方法应该是根据你的机子的IP来算的,这个具体百度是怎么做的,还有待研究。

     百度搜索用的是GB2312编码,所以利用HttpUtility.UrlEncode要做一个简单转换。

     代码

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        
    <title>天气预报</title>
        
    <style type="text/css">
            .al_wt
            
    {
                margin-top
    : 5px;
            
    }
            .al_wt td
            
    {
                font-size
    : 14px;
                line-height
    : 22px;
                text-align
    : center;
                vertical-align
    : top;
            
    }
            .al_wt td img
            
    {
                margin
    : 5px 0;
                width
    : 48px;
                height
    : 48px;
                border
    : none;
            
    }
            .al_wt td img.al_il
            
    {
                margin-right
    : 10px;
            
    }
            .al_wt td span
            
    {
                font-size
    : 13px;
            
    }
            .al_wt td div
            
    {
                text-align
    : center;
                padding
    : 0 5px;
                white-space
    : nowrap;
            
    }
            .al_wt .altime_special
            
    {
                white-space
    : nowrap;
            
    }
            .al_wt .altemp_special
            
    {
                font-size
    : 15px;
                white-space
    : nowrap;
            
    }
            .al_wt .altd_normal strong
            
    {
                font-weight
    : normal;
                font-size
    : 14px;
            
    }
            .al_wt .altd_normal .altime_special, .al_wt .altd_normal .altemp_special
            
    {
                white-space
    : normal;
            
    }
            .al_wt td.al_tr
            
    {
                padding-right
    : 20px;
            
    }
            .al_wt td.al_tl
            
    {
                padding-left
    : 20px;
                border-left
    : 1px solid #e2e9fc;
            
    }
            .al_wlink
            
    {
                font-size
    : 12px;
                color
    : #666;
                padding
    : 5px 0;
                line-height
    : 20px;
            
    }
            .al_wlink a
            
    {
                color
    : #77c;
                margin
    : 0 5px;
            
    }
        
    </style>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
            城市:
            
    <input type="text" id="txtcity" runat="server" />
            
    <input type="button" runat="server" id="Submit" value="查 询" onserverclick="On_Submit" />
        
    </div>
        
    <div id="Div_html" runat="server">
        
    </div>
        
    </form>
    </body>
    </html>
  • 相关阅读:
    干货分享:路由与交换详解大全!
    基于ASCII字符集对比
    css文字两端对齐
    软件版本号(BETA、RC、ALPHA、Release、GA等)
    install和update区别
    Blazor入坑指南
    解决Electron7.0.0的坑,cnpm install electron 安装失败的问题
    Linux查看CPU和内存使用情况
    位运算符在JS中的妙用
    centos7通过yum安装mysql
  • 原文地址:https://www.cnblogs.com/chjw8016/p/1734920.html
Copyright © 2011-2022 走看看