落伍者站长论坛's Archiver

明皓 发表于 2008-11-8 23:56

闲着,发个C#抓取alexa排名的代码.(百度GG都没有的)

网上有asp版的代码..但是没有.net的..百度GG雅虎都搜索遍了..也没有.. :ohh:
有教程站的朋友别错过啊,绝对的原创.
找遍了百度和GG,英文雅虎也找过了,没找到现成的.
只好自己写了一个,抓取很准确.^_^.需要的朋友随便拿.
(不晓得这有几位搞技术的朋友撒?)
在写一个[url=http://77521.cn/]seo[/url]的小工具,模拟蜘蛛爬取链接的,而且是检测蜘蛛认可的有效链接!!
(起因是有几个GG群的朋友遭遇到,恶意假链接欺骗,表面看谁也 看不出来,可实际蜘蛛一爬就漏馅了,用我的软件一检测,也可以原形毕露...)
废话不说了,代码在下面,附上截图一个..是我用朋友的[url=http://77521.cn/]环保网[/url]站检测的效果图,大概看下.
[img=773,234]http://www.admin5.com/upimg/userup/44093/440930F01010P0000L64.jpg[/img]
private string getAlexa(int idx, string url)
{
    //SourceCode By [url=http://77521.cn/]http://77521.cn[/url]
    string css = "";
    string result = "无排名";
    string html = Func.GetHttpPage("[url=http://www.alexa.com/search?q]http://www.alexa.com/search?q[/url]=" + url.Replace("http://",""), "utf-8");
    string patt = "<link href=\"([^\"'\\s]*?)\" _fcksavedurl="\"([^\"'\\s]*?)\"" _fcksavedurl="\"([^\"'\\s]*?)\"" type=\"text/css\" rel=\"stylesheet\">";
    Regex reg = new Regex(patt, RegexOptions.IgnoreCase);
    MatchCollection mc = reg.Matches(html);
    if (mc.Count <= 0)
    {//出错了
        dgEdit(idx, 7, "无排名");
    }
    else
    {//找到CSS了
        css = Func.GetHttpPage(mc[0].Result("$1"), "utf-8");
        reg = new Regex("Rank:([\\s\\S]*?)</a>", RegexOptions.IgnoreCase);
        mc = reg.Matches(html);
        if (mc.Count <= 0)
        {
            dgEdit(idx, 7, "无排名");
        }
        else
        {
            string mao = mc[0].Result("$1");
            string mao2 = mao.Replace("\"", "").Replace("'", "").Replace(" ", "");
            mao2 = Regex.Replace(mao2,"<\\!--.+?-->", "");
            reg = new Regex("<span[\\s\\S]*?class=[\"']?(.+?)[\"']?>(.+?)</span>", RegexOptions.IgnoreCase);
            mc = reg.Matches(mao2);
            if (mc.Count <= 0)
            {
                dgEdit(idx, 7, "无排名");
            }
            else
            {
                //textBox1.Text = mao2;
                for (int n = 0; n < mc.Count; n++)
                {
                    if (css.IndexOf(mc[n].Result("$1")) != -1)
                    {
                        mao2 = Regex.Replace(mao2, "<span[^>]*?class=" + mc[n].Result("$1") + ">.+?</span>",
                        //textBox1.Text = textBox1.Text + "\r\n\r\n------------------------------------------
                    }
                }
                mao2 = Regex.Replace(mao2, "<[\\s\\S]+?>", "");
                mao2 = Regex.Replace(mao2, " ", "");
                mao2 = Regex.Replace(mao2, " ", "");
                mao2 = Regex.Replace(mao2, "|", "");
                mao2 = mao2.Replace("\r", "").Replace("\n", "");
                dgEdit(idx, 7, mao2);
                return mao2;
            }
        }
    }
    return result;
}

再废话点,这样代码可能普通站长是没用的,发在休闲区,也是个小原创,百度GG雅虎都没相关的代码哈.
有教程类的网站的朋友,别错过啊.^_^
尽管有点不太可能,还是希望大家转载的时候能保留一个链接.
------------------------------------------------------------------------------------------------
代码里, getHttpPage 是抓取网页代码的函数.这样简单的大家应该自己就备有的.不说了.
dgEdit 是一个delegate而已.

[[i] 本帖最后由 明皓 于 2008-11-8 23:58 编辑 [/i]]

tomore 发表于 2008-11-9 00:15

不过,收藏

sctra 发表于 2008-11-9 00:26

good

正版飞云 发表于 2008-11-9 07:55

:( :( 我也不是技术型的。。。。

hnxmw 发表于 2008-11-9 14:07

很不错,可惜看不懂,占位收藏~!

djk8 发表于 2008-11-9 14:31

怎么用?

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.