获取alexa世界排名和中国排名

  直接从alexa网站a下来的数据,然后使用正则表达式分别获取alexa的世界排名和在中国的网站排名。

  示例代码点击这里查看

  下面列出的是asp.net的代码,ajax代码不列出,有兴趣自己下载示例代码里面的js文件来研究。
 

-C#
<%@ WebHandler Language="C#" Class="latestalexa" %>

using System;
using System.Web;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
public class latestalexa : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/html";
        string u = (context.Request.Form["u"] + "").Trim(), rank = "{rank:'{0}',cnrank:'{1}'}";
        if (u != "")
        {
            try
            {
                WebClient wc = new WebClient();
                string htmlString = wc.DownloadString("http://www.alexa.com/siteinfo/" + u);
                wc.Dispose();
                Match m = Regex.Match(htmlString, "<th>3\\s*month</th>\\s*<td class=\"avg\\s*\">\\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'}"; }
        }
        context.Response.Write(rank);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

加支付宝好友偷能量挖...


原创文章,转载请注明出处:获取alexa世界排名和中国排名

评论(0)Web开发网
阅读(218)喜欢(1)网站排名及优化