C#源代码-查询alexa全球,中国排名,google pr,google收录和百度收录及快照

    发一个C#源代码,可以查询alexa全球,中国排名,google pr,google收录和百度收录及快照,方便友链时查询这个网站是否被google或者百度惩罚过。
    示例效果查看alexa排名,google page rank,google收录,百度收录和百度快照,代码如下:

using System;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
namespace BLL
{
    /// <summary>
    /// 下载信息url的html内容的类
    /// </summary>
    public class Net
    {
        /// <summary>
        /// 使用webclient
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetHtml(string url)
        {
            string html = null;
            WebClient wc = new WebClient();
            try { html = wc.DownloadString(url); }
            catch { }
            wc.Dispose();
            return html;
        }
        /// <summary>
        /// 使用HttpWebRequest对象
        /// </summary>
        /// <param name="url"></param>
        /// <param name="encoding">编码</param>
        /// <returns></returns>
        public static string GetHtml(string url,Encoding encoding)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            string html = null;
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader srd = new StreamReader(response.GetResponseStream(), encoding);
                html = srd.ReadToEnd();
                srd.Close();
                response.Close();
            }
            catch { }
            return html;
        }
    }
    /// <summary>
    /// 获取google pageran的类
    /// </summary>
    public class GooglePR
    {
        private static string tryMore(string strUrl)
        {
            string sURL = "http://toolbarqueries.google.com/search?client=navclient-auto&ch=" + GetPR(strUrl) + "&ie=UTF-8&oe=UTF-8&features=Rank&q=info:" + System.Web.HttpUtility.UrlEncode(strUrl);
            string result = Net.GetHtml(sURL);
            if (result != null)
            {
                result = result.Trim();
                if (result.IndexOf(':') > 0)
                {
                    string[] pr = result.Split(':');
                    if (pr.Length == 3) return pr[2].ToString();
                }
            }
            return "0";
        }
        public static string GetPageRank(string strUrl)
        {
            string pr = "0";
            if (!string.IsNullOrEmpty(strUrl))
            {
                strUrl = strUrl.ToLower().Replace("http://", "").Replace("www.", "").Trim('/', ' ');
                pr = tryMore(strUrl);//获取等级域名
                if (pr != "0") return pr;
                if (strUrl.Split('.').Length == 2)//为顶级域名
                {
                    pr = tryMore("www." + strUrl);//获取www.域名
                    if (pr != "0") return pr;
                    pr = tryMore("http://www." + strUrl);
                    if (pr != "0") return pr;
                }
                pr = tryMore("http://" + strUrl);
            }
            return pr;
        }

        private static string GetPR(string url)
        {
            url = "info:" + url;
            string ch = GoogleCH(str_asc(url)).ToString();
            ch = "6" + ch;
            return ch;
        }

        private static int[] str_asc(string str)
        {
            if (str == null || str == string.Empty) return null;
            int[] result = new int[str.Length];
            for (int i = 0; i < str.Length; i++) result[i] = (int)str[i];
            return result;
        }

        private static long yiweitwo(long a, long b)
        {
            long z=0x80000000;
            if ((z & a) != 0)
            {
                a = (a >> 1);
                a &= (~z);
                a |= 0x40000000;
                a = ((int)a >> (int)(b - 1));
            }
            else
            {
                a = ((int)a >> (int)b);
            }
            return a;
        }

        private static int[] yiwei(long a, long b, long c)
        {
            a -= b; a -= c; a ^= (yiweitwo(c, 13));
            b -= c; b -= a; b ^= (a << 8);
            c -= a; c -= b; c ^= (yiweitwo(b, 13));
            a -= b; a -= c; a ^= (yiweitwo(c, 12));
            b -= c; b -= a; b ^= (a << 16);
            c -= a; c -= b; c ^= (yiweitwo(b, 5));
            a -= b; a -= c; a ^= (yiweitwo(c, 3));
            b -= c; b -= a; b ^= (a << 10);
            c -= a; c -= b; c ^= (yiweitwo(b, 15));
            return new int[] { (int)a, (int)b, (int)c };

        }

        private static int GoogleCH(int[] url)
        {
            int length=url.Length;
            long a=0x9E3779B9;
            long b=0x9E3779B9;
            long c=0xE6359A60;
            int k=0;
            int len=length;
            int[] mid;
            while (len >= 12)
            {
                a += (url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24));
                b += (url[k + 4] + (url[k + 5] << 8) + (url[k + 6] << 16) + (url[k + 7] << 24));
                c += (url[k + 8] + (url[k + 9] << 8) + (url[k + 10] << 16) + (url[k + 11] << 24));
                mid=yiwei(a, b, c);
                a=mid[0]; b=mid[1]; c=mid[2];
                k += 12;
                len -= 12;
            }
            c += length;
            switch (len)
            {
                case 11:
                    {
                        c += (url[k + 10] << 24);
                        c += (url[k + 9] << 16);
                        c += (url[k + 8] << 8);
                        b += (url[k + 7] << 24);
                        b += (url[k + 6] << 16);
                        b += (url[k + 5] << 8);
                        b += (url[k + 4]);
                        a += (url[k + 3] << 24);
                        a += (url[k + 2] << 16);
                        a += (url[k + 1] << 8);
                        a += (url[k + 0]);
                        break;
                    }
                case 10:
                    {
                        c += (url[k + 9] << 16);
                        c += (url[k + 8] << 8);
                        b += (url[k + 7] << 24);
                        b += (url[k + 6] << 16);
                        b += (url[k + 5] << 8);
                        b += (url[k + 4]);
                        a += (url[k + 3] << 24);
                        a += (url[k + 2] << 16);
                        a += (url[k + 1] << 8);
                        a += (url[k + 0]);
                        break;
                    }

                case 9:
                    {
                        c += (url[k + 8] << 8);
                        b += (url[k + 7] << 24);
                        b += (url[k + 6] << 16);
                        b += (url[k + 5] << 8);
                        b += (url[k + 4]);
                        a += (url[k + 3] << 24);
                        a += (url[k + 2] << 16);
                        a += (url[k + 1] << 8);
                        a += (url[k + 0]);
                        break;

                    }

                case 8:
                    {
                        b += (url[k + 7] << 24);
                        b += (url[k + 6] << 16);
                        b += (url[k + 5] << 8);
                        b += (url[k + 4]);
                        a += (url[k + 3] << 24);
                        a += (url[k + 2] << 16);
                        a += (url[k + 1] << 8);
                        a += (url[k + 0]);
                        break;

                    }

                case 7:
                    {

                        b += (url[k + 6] << 16);
                        b += (url[k + 5] << 8);
                        b += (url[k + 4]);
                        a += (url[k + 3] << 24);
                        a += (url[k + 2] << 16);
                        a += (url[k + 1] << 8);
                        a += (url[k + 0]);
                        break;

                    }

                case 6:
                    {
                        b += (url[k + 5] << 8);
                        b += (url[k + 4]);
                        a += (url[k + 3] << 24);
                        a += (url[k + 2] << 16);
                        a += (url[k + 1] << 8);
                        a += (url[k + 0]);
                        break;
                    }

                case 5:
                    {
                        b += (url[k + 4]);
                        a += (url[k + 3] << 24);
                        a += (url[k + 2] << 16);
                        a += (url[k + 1] << 8);
                        a += (url[k + 0]);
                        break;

                    }

                case 4:
                    {
                        a += (url[k + 3] << 24);
                        a += (url[k + 2] << 16);
                        a += (url[k + 1] << 8);
                        a += (url[k + 0]);
                        break;

                    }

                case 3:
                    {
                        a += (url[k + 2] << 16);
                        a += (url[k + 1] << 8);
                        a += (url[k + 0]);
                        break;
                    }
                case 2:
                    {
                        a += (url[k + 1] << 8);
                        a += (url[k + 0]);
                        break;
                    }

                case 1:
                    {
                        a += (url[k + 0]);
                        break;
                    }

            }
            mid=yiwei(a, b, c);
            return mid[2];
        }
    }
    ///
    /// 获取Alexa排名
    /// </summary>
    public class Alexa
    {
        /// <summary>
        /// 通过API接口获取数据,只能获取全球排名
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        public static string API(string u)
        {
            string rank = "NaN";
            try
            {
                string xmlString = BLL.Net.GetHtml("http://data.alexa.com/data/+wQ411en8000lA?cli=10&dat=snba&ver=7.0&cdt=alx_vw=20&"
                    + "wid=12206&act=00000000000&ss=1680x1050&bw=964&t=0&ttl=35371&vis=1&rq=4&url=" + u);
                Match m = Regex.Match(xmlString, "<POPULARITY URL="s*.+?s*" TEXT="s*(d+)s*"/>"
                    , RegexOptions.IgnoreCase | RegexOptions.Compiled);
                if (m.Groups[1].Value != "") rank = m.Groups[1].Value;
            }
            catch { }
            return rank;
        }
        /// <summary>
        /// 直接从alexa的网站下载html代码分析,获取中国及世界排名,返回内容为json
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        public static string Latest(string u)
        {
            string rank = "{rank:'{0}',cnrank:'{1}'}";
            try
            {
                string htmlString = BLL.Net.GetHtml("http://www.alexa.com/siteinfo/" + u);
                Match m = Regex.Match(htmlString, "<th>3s*month</th>s*<td class="avgs*">s*([d,]+)s*</td>"
                    , RegexOptions.IgnoreCase | RegexOptions.Compiled);
                rank = rank.Replace("{0}", m.Groups[1].Value);
                m = Regex.Match(htmlString, "images/flags/cn.png[^>]+>s*([d,]+)s*</div>"
                    , RegexOptions.IgnoreCase | RegexOptions.Compiled);
                rank = rank.Replace("{1}", m.Groups[1].Value);
            }
            catch { rank = "{rank:'NaN',cnrank:'NaN'}"; }
            return rank;
        }
    }
    /// <summary>
    /// 获取google和百度收录数量,为百时同时返回快找时间
    /// </summary>
    public class GoogleBaiduIndex
    {
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="u"></param>
        /// <param name="isGG"></param>
        /// <param name="isJson">是否返回json格式的数据,否则返回用|分隔的数据</param>
        /// <returns></returns>
        public static string IndexGoogleBaidu(string u, bool isGG, bool isJson)
        {
            string rst = "", html = "";
            u = (isGG ? "http://www.google.com.hk/search?q=" : "http://www.baidu.com/s?wd=") + "site%3A" + u;
            html = BLL.Net.GetHtml(u, isGG ? Encoding.GetEncoding(950) : Encoding.GetEncoding(936));
            if (html != null)
            {//找到约 4,210 条结果
                Regex r = new Regex(isGG ? @"([d,]+)s*項結果" : @"找到相关结果约?s*([d,]+)s*个", RegexOptions.Compiled);
                Match m = r.Match(html);
                if (isGG) rst = isJson ? "{gg:'" + m.Groups[1].Value + "'}" : m.Groups[1].Value;
                else rst = isJson ? "{bd:'" + m.Groups[1].Value + "',bdTime:'"
                    + Regex.Match(html, @"/s*(d{4}(-d{1,2}){2})", RegexOptions.Compiled).Groups[1].Value + "'}"
                : m.Groups[1].Value + "|" + Regex.Match(html, @"/s*(d{4}(-d{1,2}){2})", RegexOptions.Compiled).Groups[1].Value;
            }
            else rst = "{success:false}";
            return rst;
        }
    }
}

by 雪洁 2011-06-07 15:44:27 3175 views
我来说几句

相关文章