c# 调用Google查询天气 手机所属地 农历转换公历

本文介绍了一种利用Google搜索API抓取特定信息的方法,包括天气预报、手机号归属地及农历转公历等。通过编码实现了网页数据下载与解析,能够有效获取所需数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

红色字替换您需要的信息。

 

//获取最近三天天气情况
        string url = string.Format("http://www.google.cn/search?hl=zh-CN&q=tq{0}&meta=&aq=f&oq=", HttpUtility.UrlEncode("上海 ", System.Text.Encoding.UTF8).ToUpper());

        byte[] bytes = new System.Net.WebClient().DownloadData(url);

        string html = System.Text.Encoding.Default.GetString(bytes);

        int a = html.IndexOf("<div class=e>");
        if (a > 0)
        {

            a += 13;

            int b = html.IndexOf("<h2 class=hd>", a);
            if (b > a)
            {

                Response.Write(html.Substring(a - 13, b - a + 13).Replace("src=/"/", "src=/"http://www.google.cn/"));

                return;

            }

        }

        //第一种:获取手机号所属地
        string url = string.Format("http://www.google.cn/search?hl=zh-CN&q={0}&aq=f&oq=", "手机号 ");
        byte[] bytes = new System.Net.WebClient().DownloadData(url);
        string html = System.Text.Encoding.Default.GetString(bytes);
        int a = html.IndexOf("<div class=rbt>");
        if (a > 0)
        {
            a += 15;
            int b = html.IndexOf("<li class=g>", a);
            if (b > a)
            {
                Response.Write(html.Substring(a - 15, b - a + 15).Replace("src=/"/", "src=/"http://www.google.cn/").Replace("href=/"/url", "href=/"http://www.google.cn/url"));
                return;
            }
        }

 

/////////////////////////// 第二种:无标题页四川 南充 - 中国联通

        string url = string.Format("http://www.google.cn/search?hl=zh-CN&q={0}&aq=f&oq=", "13281906391");
        byte[] bytes = new System.Net.WebClient().DownloadData(url);
        string html = System.Text.Encoding.Default.GetString(bytes);
        int a = html.IndexOf("归属地查询</a></h3>&nbsp;");
        if (a > 0)
        {
            a += 20;
            int b = html.IndexOf("<div style=/"margin:2px 0/">", a);
            if (b > a)
            {
                Response.Write(html.Substring(a, b - a));
                return;
            }
        }
        //农历转换公历
        string url = string.Format("http://www.google.cn/search?hl=zh-CN&q={0}&aq=f&oq=", HttpUtility.UrlEncode("n l1981-6-5 ", System.Text.Encoding.UTF8).ToUpper());
        byte[] bytes = new System.Net.WebClient().DownloadData(url);
        string html = System.Text.Encoding.Default.GetString(bytes);
        int a = html.IndexOf("<div class=e>");
        if (a > 0)
        {
            a += 13;
            int b = html.IndexOf("</table></table></div></div>", a);
            if (b > a)
            {
                Response.Write(html.Substring(a - 13, b - a + 29).Replace("src=/"/", "src=/"http://www.google.cn/"));
                return;
            }
        }

 

 

//通过sogou查询IP地址

        string url = string.Format("http://www.sogou.com/web?query={0}&ast=&asf=&w=&num=&p=&dp=", "123.112.11.60");

        byte[] bytes = new System.Net.WebClient().DownloadData(url);

        string html = System.Text.Encoding.Default.GetString(bytes);

        int a = html.IndexOf("<p class=/"ff/">");
        if (a > 0)
        {

            a += 14;

            int b = html.IndexOf("</p>", a);
            if (b > a)
            {
                Response.Write(html.Substring(a - 14, b - a + 14));

                return;
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值