代码改变世界

c# 获取百度、谷歌关键字排名

2010-09-16 16:05  xchit  阅读(1627)  评论(0编辑  收藏  举报

//前台

<!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 id="Head1" runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="百度关键字排名查询"></asp:Label>
        &nbsp;<br />
        关键字:
        <asp:TextBox ID="TextBox1" runat="server" Width="308px">c# 获取百度、谷歌关键字排名</asp:TextBox>
        <br />
        网址:<asp:TextBox ID="TextBox2" runat="server" Width="331px">cnblogs.com</asp:TextBox>
        如输入首页域名,包括最后的 “/”<br />
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp;
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="查询" />
        <asp:Label ID="lbBaiduResult" runat="server"></asp:Label><br />
        <br />
        google关键字排名查询<br />
        关键字:<asp:TextBox ID="TextBox3" runat="server" Width="316px">c# 获取百度、谷歌关键字排名</asp:TextBox><br />
        网址:<asp:TextBox ID="TextBox4" runat="server" Width="331px">cnblogs.com</asp:TextBox><br />
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp;
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="查询" />
        <asp:Label ID="lbGoogleResult" runat="server"></asp:Label><br />
    </div>
    </form>
</body>
</html>

//后台代码

 

 protected void Button1_Click(object sender, EventArgs e)
        {
            int paiMing = 0;

            string urlWord = HttpUtility.UrlEncode(TextBox1.Text, Encoding.GetEncoding("gb2312"));

            for (int i = 0; i < 500; i += 10)
            {
                string queryUrl = "http://www.baidu.com/s?lm=0&si=&rn=10&ie=gb2312&ct=0&wd=" + urlWord + "&pn=" + i.ToString() + "&ver=0&cl=3";
                WebRequest request = WebRequest.Create(queryUrl);
                WebResponse response = request.GetResponse();
                Stream resStream = response.GetResponseStream();
                StreamReader sr = new StreamReader(resStream, Encoding.Default);
                string htmlCode = sr.ReadToEnd();
                resStream.Close();
                sr.Close();
                int startPos = htmlCode.IndexOf("<font color=\"#008000\" size=\"-1\">e.baidu.com</font></a>");
                int endPos = htmlCode.IndexOf("<br clear=all>");
                string result = htmlCode.Substring(startPos, endPos - startPos);
                string[] info = SplitString(result, "百度快照</a>");
                int ret = getBaiduPaiMing(info, "cnblogs.com");
                if (ret != 0)
                {
                    paiMing += ret;
                    break;
                }
                else
                    paiMing += 10;
            }
            if (paiMing == 0 || paiMing > 500)
                lbBaiduResult.Text = "没有排名";
            else
                lbBaiduResult.Text = "排名:" + paiMing.ToString();
        }

 

        protected void Button2_Click(object sender, EventArgs e)
        {
            int paiMing = 0;
            string urlWord = HttpUtility.UrlEncode(TextBox3.Text, Encoding.GetEncoding("gb2312"));
            for (int i = 0; i < 500; i += 10)
            {
                string queryUrl = "http://www.google.com.hk/search?hl=zh-CN&newwindow=1&q=" + urlWord + "&start=" + i.ToString() + "&sa=N";
                WebRequest request = WebRequest.Create(queryUrl);
                WebResponse response = request.GetResponse();
                Stream resStream = response.GetResponseStream();
                StreamReader sr = new StreamReader(resStream, Encoding.Default);
                string htmlCode = sr.ReadToEnd();
                resStream.Close();
                sr.Close();
                int startPos = htmlCode.IndexOf("<div id=ires>");
                int endPos = htmlCode.IndexOf("<div id=leftnav");
                string result = htmlCode.Substring(startPos, endPos - startPos);
                string[] info = SplitString(result, ">网页快照</a>");
                int ret = getGooglePaiMing(info, "cnblogs.com");
                if (ret != 0)
                {
                    paiMing += ret;
                    break;
                }
                else
                    paiMing += 10;
            }
            if (paiMing == 0 || paiMing > 500)
                lbGoogleResult.Text = "没有排名";
            else
                lbGoogleResult.Text = "排名:" + paiMing.ToString();
        }


        static public int getBaiduPaiMing(string[] info, string urlStr)
        {
            for (int i = 0; i < info.Length; i++)
            {
                int m = info[i].IndexOf("href=\"http://") + 6;
                int n = info[i].IndexOf("target=\"_blank\" ><font") - 2;
                string ret = info[i].Substring(m, n - m);
                if (ret.IndexOf(urlStr) > -1)
                    return i + 1;
            }
            return 0;
        }
        static public int getGooglePaiMing(string[] info, string urlStr)
        {
            for (int i = 0; i < info.Length; i++)
            {
                int m = info[i].IndexOf("<h3 class=r><a href=\"http://") + 21;
                int n = info[i].IndexOf("target=_blank class=l", m);   // info[i].IndexOf("target=_blank class=l") - 2;
                string ret = info[i].Substring(m, n - m - 2);

                if (ret.IndexOf(urlStr) > -1)
                    return i + 1;
            }
            return 0;
        }
        static public string[] SplitString(string str, string separator)
        {
            string tmp = str;
            Hashtable ht = new Hashtable();
            int i = 0;
            int pos = tmp.IndexOf(separator);
            while (pos != -1)
            {
                ht.Add(i, tmp.Substring(0, pos));
                tmp = tmp.Substring(pos + separator.Length);
                pos = tmp.IndexOf(separator);
                i++;
            }
            ht.Add(i, tmp);
            string[] array = new string[10];
            for (int j = 0; j < 10; j++)
                array[j] = ht[j].ToString();

            return array;
        }