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