get alexa info
#!/usr/bin/env python
import sys
import re
import urllib2
def get_alexa_rank(url):
try:
global country_code
data = urllib2.urlopen('http://data.alexa.com/data?cli=10&dat=snbamz&url=%s' % (url)).read()
reach_rank = re.findall("REACH[^\d].*TEXT=\"(\d+)", data)
if reach_rank: reach_rank = reach_rank[0]
else: reach_rank = -1
popularity_rank = re.findall("POPULARITY[^\d]*(\d+)", data)
if popularity_rank: popularity_rank = popularity_rank[0]
else: popularity_rank = -1
country_code_rank = re.findall("COUNTRY CODE=\"(.*)\" NAME=[^\d]*(\d+)",data)
country = country_code_rank[0]
country_rank = country[-1]
country_code = country[0]
return int(popularity_rank), int(reach_rank), int(country_rank)
except (KeyboardInterrupt, SystemExit):
raise
except:
return None
country_code =""
if __name__ == '__main__':
if len(sys.argv) < 2:
print 'usage: python %s <site-url>' % (sys.argv[0])
sys.exit(2)
url = sys.argv[1]
data = get_alexa_rank(url)
popularity_rank, reach_rank, country_rank = -1, -1, -1
if data:
popularity_rank, reach_rank, country_rank= data
print 'popularity_rank = %d\nreach_rank = %d\n%s_rank = %d' % (popularity_rank, reach_rank,country_code,country_rank)