Results 1 to 4 of 4

Thread: Check Alexa Ranking of a Domain - "alexa.py" Share/Save - My123World.Com!

  1. #1
    Garage Member
    Join Date
    Aug 2012
    Location
    India
    Posts
    97
    Blog Entries
    1

    Check Alexa Ranking of a Domain - "alexa.py"

    Hello,

    I just wrote a quick and dirty solution to check Alexa ranking of a domain. So, I thought would share the script with you guys. Might help someone.

    Note: This script uses BeautifulSoup to parse the webpage for extracting ranks. This script might stop working in future if structure of Alexa's website is changed. Also, you should have BeautifulSoup 4 and Requests libraries. And, this script prints the global ranking and single county rank, without changing anything from wepage. Might not work as expected and may cause Gotham to fall into clutches of Joker (I would hate that though.). Well If you still insist, here's the code:

    Code:
    #!/usr/bin/env python
    # Description: A quick script to check a domain's Alexa rank.
    # Author: c0dist
    # Usage: python alexa.py <domain_to_query>
    
    import sys
    import requests
    from bs4 import BeautifulSoup as bs
    
    def get_rank(domain_to_query):
        result = {'Global':''}
        url = "http://www.alexa.com/siteinfo/" + domain_to_query
        page = requests.get(url).text
        soup = bs(page)
        for span in soup.find_all('span'):
            if span.has_attr("class"):
                if "globleRank" in span["class"]:
                    for strong in span.find_all("strong"):
                        if strong.has_attr("class"):
                            if "metrics-data" in strong["class"]:
                                result['Global'] = strong.text
                # Extracting CountryRank
                if "countryRank" in span["class"]:
                    image = span.find_all("img")
                    for img in image:
                        if img.has_attr("title"):
                            country = img["title"]
                    for strong in span.find_all("strong"):
                        if strong.has_attr("class"):
                            if "metrics-data" in strong["class"]:
                                result[country] = strong.text
        return result
    
    if __name__ == '__main__':
        print get_rank(sys.argv[1])

    Usage:
    Code:
    batman@gothamcity ~ $ python alexa.py google.com
    {'Global': u'1', 'United States Flag': u'1'}
    Using inside another python script:
    Code:
    #!/usr/bin/env python
    import alexa
    
    print alexa.get_rank("garage4hackers.com") # Yup, just like this. ;) Returns a dictionary. You can parse accordingly.
    Share your suggestions and comments (if any)

    Regards,
    c0dist
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who
    keeps learning stays young. The greatest thing in life is to keep your mind young.
    - Henry Ford

  2. #2
    Garage Member
    Join Date
    Aug 2012
    Location
    India
    Posts
    97
    Blog Entries
    1
    Also, while browsing forum, came across this Perl script by silentph33r which does the same work plus reverse IP lookup. Though haven't tested the script.

    POst link - http://garage4hackers.com/content.ph...a-Rank-Checker
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who
    keeps learning stays young. The greatest thing in life is to keep your mind young.
    - Henry Ford

  3. #3
    ... I am no Expert b0nd.g4h@gmail.com b0nd's Avatar
    Join Date
    Jul 2010
    Location
    irc.freenode.net #g4h
    Posts
    744
    Thanks for sharing it! Open source is always welcome!

    Cheers!
    [*] To follow the path: look to the master, follow the master, walk with the master, see through the master,
    ------> become the master!!! <------
    [*] Everyone has a will to WIN but very few have the will to prepare to WIN
    [*] Invest yourself in everything you do, there's fun in being serious

  4. #4
    Garage Member
    Join Date
    Aug 2012
    Location
    India
    Posts
    97
    Blog Entries
    1
    For future reference, here's the script on my Github. If I add something or fix something, please check here - https://github.com/RahulBinjve/scrip...aster/alexa.py
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who
    keeps learning stays young. The greatest thing in life is to keep your mind young.
    - Henry Ford

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •