domingo, 30 de enero de 2011

[C#] Get Alexa Page Rank Programmatically


So basically we if we want to get the Alexa Page Rank for
a certain url, we have to access their xml API, (I'm sure there's
JSON outhere) and pass the url via querystring, and then just parse
the response.

Here is how:


public static int getAlexa(String URL) {
    int AlexaRank = 0;
    WebRequest request = WebRequest.Create("http://data.alexa.com/data? cli=10&dat=snbamz&url=" + URL);
    WebResponse response = request.GetResponse();
    StreamReader sr = new StreamReader(response.GetResponseStream());
    string line = "";
    while ((line = sr.ReadLine()) != null) {
        Regex rankRegex = new Regex("");
        Match match = rankRegex.Match(line);
        if (match.Success) {
            AlexaRank = int.Parse(match.Groups[1].Value);
        }
    }
    sr.Close();
    return AlexaRank;
}

and there you go, you can also convert this int, to string
or whatever, nothing complex.

Hope you find this code snippet usefull :)

6 comentarios:

  1. try making a regex for the following line in the response:



    to get the "TEXT=..." value, and you should get the alexa rank

    ResponderEliminar
  2. Yes, I saw there was no regex. Do you have it? I guess I will make it.

    ResponderEliminar
  3. it spits out xml here, http://data.alexa.com/data?cli=10&dat=snbamz&url=google.com

    ResponderEliminar
  4. yes, I'm sorry about the reges, I forgot that (lazy me...).

    or, you can use an xml parser as well, and get the node you want, the XmlDocument class should do the trick.

    ResponderEliminar
  5. Website Alexa Ranks can be very easily fetched using PowerShell. See this -
    https://ridicurious.com/2018/09/21/how-to-check-alexa-ranking-using-powershell/

    ResponderEliminar