http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 8009|回复: 3

[综合] C#实现google Pr查询的方法

[复制链接]
发表于 2014-2-8 16:14:41 | 显示全部楼层 |阅读模式
C#实现google Pr查询的方法

具体的大家可以看我的网站
http://tool.sufeinet.com/pr.aspx
QQ截图20140208161202.jpg
上面圈起来的2就是Pr值
有人在群里问我网站的Pr查询工具是怎么实现的,
下面我就把源码开放出来让大家看看。
界面代码的
实现方法如下
[C#] 纯文本查看 复制代码
 try
            {
                if (!IsPostBack)
                {

                    if (Request["domain"] != null)
                    {
                        string domain = Request["domain"];
                        GetPr pr = new GetPr();
                        txtpr.InnerHtml = pr.getpriframe(domain);
                        if ( string.IsNullOrEmpty(txtpr.InnerHtml))
                        {
                             txtpr.InnerHtml = "没有查询到PR值";
                        }
                        txtquerypr.Value = domain;
                        title = domain + "的PR查询," + title;
                    }
                    else
                    {
                        txtpr.InnerHtml = "没有查询到PR值";
                    }
                }

            }
            catch (Exception ex)
            {
                txtpr.InnerHtml = ex.Message;

            }

GetPr类如下显示
[C#] 纯文本查看 复制代码
/// <summary>
/// 类说明:GetPr类,
/// 编码日期:2011-08-22
/// 编 码 人:  苏飞
/// 联系方式:361983679  
/// </summary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using tools;
using System.Text;
using DotNet.Utilities;
namespace tools
{
    /// <summary>
    ///GetPr 的摘要说明
    /// </summary>
    public class GetPr
    {
        public GetPr()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        /// <summary>
        /// 查询得到框架页面
        /// </summary>
        /// <param name="url">查询URL</param>
        /// <param name="number">0是先查询库次查询框架;1是直接查询getpr方法</param>
        /// <returns></returns>
        public string getpriframe(string url)
        {
            url = FunctionServices.GetUrlRoot(url);
            string s = CalculateChecksum(url);
            string returnstr = "http://toolbarqueries.google.com/tbr?client=navclient-auto&googleip=O;937&ch=" +
                           s + "&ie=UTF-8&oe=UTF-8&features=Rank:FVN&q=info:" + url;
            HttpHelper http = new HttpHelper();
            //新网查询
            HttpItem item = new HttpItem() { URL=returnstr};
            return http.GetHtml(item).Html;
        }


        uint GOOGLE_MAGIC = 0xE6359A60;

        private uint zeroFill(uint a, int b)
        {
            checked
            {
                uint z = 0x80000000;
                if (Convert.ToBoolean(z & a))
                {
                    a = (a >> 1);
                    a &= (~z);
                    a |= 0x40000000;
                    a = (a >> (b - 1));
                }
                else
                {
                    a = (a >> b);
                }
            }
            return a;
        }

        private uint[] mix(uint a, uint b, uint c)
        {
            a -= b; a -= c; a ^= (uint)(zeroFill(c, 13));
            b -= c; b -= a; b ^= (uint)(a << 8);
            c -= a; c -= b; c ^= (uint)(zeroFill(b, 13));
            a -= b; a -= c; a ^= (uint)(zeroFill(c, 12));
            b -= c; b -= a; b ^= (uint)(a << 16);
            c -= a; c -= b; c ^= (uint)(zeroFill(b, 5));
            a -= b; a -= c; a ^= (uint)(zeroFill(c, 3));
            b -= c; b -= a; b ^= (uint)(a << 10);
            c -= a; c -= b; c ^= (uint)(zeroFill(b, 15));

            return new uint[3] { a, b, c };
        }

        private uint GoogleCH(uint[] url, uint length, uint init)
        {
            if (length == 0)
            {
                length = (uint)url.Length;
            }
            uint a, b;
            a = b = 0x9E3779B9;
            uint c = init;
            int k = 0;
            uint len = length;
            uint[] m_mix = new uint[3];
            while (len >= 12)
            {
                a += (uint)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24));
                b += (uint)(url[k + 4] + (url[k + 5] << 8) + (url[k + 6] << 16) + (url[k + 7] << 24));
                c += (uint)(url[k + 8] + (url[k + 9] << 8) + (url[k + 10] << 16) + (url[k + 11] << 24));
                m_mix = mix(a, b, c);
                a = m_mix[0]; b = m_mix[1]; c = m_mix[2];

                k += 12;
                len -= 12;
            }

            c += length;

            switch (len)              /* all the case statements fall through */
            {
                case 11:
                    {
                        c += (uint)(url[k + 10] << 24);
                        c += (uint)(url[k + 9] << 16);
                        c += (uint)(url[k + 8] << 8);
                        b += (uint)(url[k + 7] << 24);
                        b += (uint)(url[k + 6] << 16);
                        b += (uint)(url[k + 5] << 8);
                        b += (uint)(url[k + 4]);
                        a += (uint)(url[k + 3] << 24);
                        a += (uint)(url[k + 2] << 16);
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                case 10:
                    {
                        c += (uint)(url[k + 9] << 16);
                        c += (uint)(url[k + 8] << 8);
                        b += (uint)(url[k + 7] << 24);
                        b += (uint)(url[k + 6] << 16);
                        b += (uint)(url[k + 5] << 8);
                        b += (uint)(url[k + 4]);
                        a += (uint)(url[k + 3] << 24);
                        a += (uint)(url[k + 2] << 16);
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                case 9:
                    {
                        c += (uint)(url[k + 8] << 8);
                        b += (uint)(url[k + 7] << 24);
                        b += (uint)(url[k + 6] << 16);
                        b += (uint)(url[k + 5] << 8);
                        b += (uint)(url[k + 4]);
                        a += (uint)(url[k + 3] << 24);
                        a += (uint)(url[k + 2] << 16);
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                /* the first byte of c is reserved for the length */
                case 8:
                    {
                        b += (uint)(url[k + 7] << 24);
                        b += (uint)(url[k + 6] << 16);
                        b += (uint)(url[k + 5] << 8);
                        b += (uint)(url[k + 4]);
                        a += (uint)(url[k + 3] << 24);
                        a += (uint)(url[k + 2] << 16);
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                case 7:
                    {
                        b += (uint)(url[k + 6] << 16);
                        b += (uint)(url[k + 5] << 8);
                        b += (uint)(url[k + 4]);
                        a += (uint)(url[k + 3] << 24);
                        a += (uint)(url[k + 2] << 16);
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                case 6:
                    {
                        b += (uint)(url[k + 5] << 8);
                        b += (uint)(url[k + 4]);
                        a += (uint)(url[k + 3] << 24);
                        a += (uint)(url[k + 2] << 16);
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                case 5:
                    {
                        b += (uint)(url[k + 4]);
                        a += (uint)(url[k + 3] << 24);
                        a += (uint)(url[k + 2] << 16);
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                case 4:
                    {
                        a += (uint)(url[k + 3] << 24);
                        a += (uint)(url[k + 2] << 16);
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                case 3:
                    {
                        a += (uint)(url[k + 2] << 16);
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                case 2:
                    {
                        a += (uint)(url[k + 1] << 8);
                        a += (uint)(url[k + 0]);
                        break;
                    }
                case 1:
                    {
                        a += (uint)(url[k + 0]);
                        break;
                    }
                /* case 0: nothing left to add */
            }
            m_mix = mix(a, b, c);
            /*-------------------------------------------- report the result */
            return m_mix[2];
        }

        private uint GoogleCH(string url, uint length)
        {
            uint[] m_urluint = new uint[url.Length];
            for (int i = 0; i < url.Length; i++)
            {
                m_urluint[i] = url[i];
            }
            return GoogleCH(m_urluint, length, GOOGLE_MAGIC);
        }

        private uint GoogleCH(string sURL)
        {
            return GoogleCH(sURL, 0);
        }

        private uint GoogleCH(uint[] url, uint length)
        {
            return GoogleCH(url, length, GOOGLE_MAGIC);
        }

        private uint[] c32to8bit(uint[] arr32)
        {
            uint[] arr8 = new uint[arr32.GetLength(0) * 4 + 3];

            for (int i = 0; i < arr32.GetLength(0); i++)
            {
                for (int bitOrder = i * 4; bitOrder <= i * 4 + 3; bitOrder++)
                {
                    arr8[bitOrder] = arr32[i] & 255;
                    arr32[i] = zeroFill(arr32[i], 8);
                }
            }
            return arr8;
        }

        //新算法,ToolBar 版本>>=2.0.114
        public string CalculateChecksum(string sURL)
        {
            uint ch = GoogleCH("info:" + sURL);

            ch = (((ch / 7) << 2) | (((uint)(ch % 13)) & 7));

            uint[] prbuf = new uint[20];
            prbuf[0] = ch;
            for (int i = 1; i < 20; i++)
            {
                prbuf[i] = prbuf[i - 1] - 9;
            }
            ch = GoogleCH(c32to8bit(prbuf), 80);

            return string.Format("6{0}", ch);
        }

        //老算法,ToolBar 版本<2.0.114
        public string CalculateChecksumOld(string sURL)
        {
            uint ch = GoogleCH("info:" + sURL);

            string CalculateChecksum = "6" + Convert.ToString((ch));
            return CalculateChecksum;
        }
    }
}

好了大家直接复制代码就可以进行查询了。
也可以到我的网站去进行在线的测试

本帖被以下淘专辑推荐:



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-2-8 19:27:12 | 显示全部楼层
很好啊,  以后用得上再来细读~
发表于 2014-2-8 21:23:42 | 显示全部楼层
受教了,学习中……
发表于 2014-4-17 10:29:43 | 显示全部楼层
哇,这个好。我做了一个小工具站,真好可以用上。
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-11-15 17:20

© 2014-2021

快速回复 返回顶部 返回列表