-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalexa_top_sites.py
More file actions
executable file
·177 lines (165 loc) · 5.34 KB
/
alexa_top_sites.py
File metadata and controls
executable file
·177 lines (165 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python
import argparse
import requests
import re
import sys
# insert the result page number you want; first page is 0
ALEXA_GLOBAL_URL = 'http://www.alexa.com/topsites/global;%d'
ALEXA_COUNTRY_URL = 'http://www.alexa.com/topsites/countries;%d/%s'
COUNTRIES = [
(('afghanistan','af'), 'AF'),
(('albania','al'), 'AL'),
(('algeria','dz'), 'DZ'),
(('argentina','ar'), 'AR'),
(('armenia','am'), 'AM'),
(('australia','au'), 'AU'),
(('austria','at'), 'AT'),
(('azerbaijan','az'), 'AZ'),
(('bahamas','bs'), 'BS'),
(('bahrain','bh'), 'BH'),
(('bangladesh','bd'), 'BD'),
(('belarus','by'), 'BY'),
(('belgium','be'), 'BE'),
(('bolivia','bo'), 'BO'),
(('bosniaandherzegovina','ba'), 'BA'),
(('brazil','br'), 'BR'),
(('bulgaria','bg'), 'BG'),
(('cambodia','kh'), 'KH'),
(('canada','ca'), 'CA'),
(('chile','cl'), 'CL'),
(('china','cn'), 'CN'),
(('colombia','co'), 'CO'),
(('costarica','cr'), 'CR'),
(('croatia','hr'), 'HR'),
(('cyprus','cy'), 'CY'),
(('czechrepublic','cz'), 'CZ'),
(('denmark','dk'), 'DK'),
(('dominicanrepublic','do'), 'DO'),
(('ecuador','ec'), 'EC'),
(('egypt','eg'), 'EG'),
(('elsalvador','sv'), 'SV'),
(('estonia','ee'), 'EE'),
(('finland','fi'), 'FI'),
(('france','fr'), 'FR'),
(('georgia','ge'), 'GE'),
(('germany','de'), 'DE'),
(('ghana','gh'), 'GH'),
(('greece','gr'), 'GR'),
(('guatemala','gt'), 'GT'),
(('honduras','hn'), 'HN'),
(('hongkong','hk'), 'HK'),
(('hungary','hu'), 'HU'),
(('iceland','is'), 'IS'),
(('india','in'), 'IN'),
(('indonesia','id'), 'ID'),
(('iran','ir'), 'IR'),
(('iraq','iq'), 'IQ'),
(('ireland','ie'), 'IE'),
(('israel','il'), 'IL'),
(('italy','it'), 'IT'),
(('jamaica','jm'), 'JM'),
(('japan','jp'), 'JP'),
(('jordan','jo'), 'JO'),
(('kazakhstan','kz'), 'KZ'),
(('kenya','ke'), 'KE'),
(('kuwait','kw'), 'KW'),
(('kyrgyzstan','kg'), 'KG'),
(('latvia','lv'), 'LV'),
(('lebanon','lb'), 'LB'),
(('libya','ly'), 'LY'),
(('lithuania','lt'), 'LT'),
(('luxembourg','lu'), 'LU'),
(('macedonia','mk'), 'MK'),
(('madagascar','mg'), 'MG'),
(('malaysia','my'), 'MY'),
(('malta','mt'), 'MT'),
(('mauritania','mr'), 'MR'),
(('mauritius','mu'), 'MU'),
(('mexico','mx'), 'MX'),
(('moldova','md'), 'MD'),
(('mongolia','mn'), 'MN'),
(('montenegro','me'), 'ME'),
(('morocco','ma'), 'MA'),
(('nepal','np'), 'NP'),
(('netherlands','nl'), 'NL'),
(('newzealand','nz'), 'NZ'),
(('nicaragua','ni'), 'NI'),
(('nigeria','ng'), 'NG'),
(('norway','no'), 'NO'),
(('oman','om'), 'OM'),
(('pakistan','pk'), 'PK'),
(('palestinianterritory','ps'), 'PS'),
(('panama','pa'), 'PA'),
(('paraguay','py'), 'PY'),
(('peru','pe'), 'PE'),
(('philippines','ph'), 'PH'),
(('poland','pl'), 'PL'),
(('portugal','pt'), 'PT'),
(('puertorico','pr'), 'PR'),
(('qatar','qa'), 'QA'),
(('reunion','re'), 'RE'),
(('romania','ro'), 'RO'),
(('russia','ru'), 'RU'),
(('saudiarabia','sa'), 'SA'),
(('senegal','sn'), 'SN'),
(('serbia','rs'), 'RS'),
(('singapore','sg'), 'SG'),
(('slovakia','sk'), 'SK'),
(('slovenia','si'), 'SI'),
(('southafrica','za'), 'ZA'),
(('southkorea','kr'), 'KR'),
(('spain','es'), 'ES'),
(('srilanka','lk'), 'LK'),
(('sudan','sd'), 'SD'),
(('sweden','se'), 'SE'),
(('switzerland','ch'), 'CH'),
(('syrianarabrepublic','sy'), 'SY'),
(('taiwan','tw'), 'TW'),
(('tanzania','tz'), 'TZ'),
(('thailand','th'), 'TH'),
(('trinidadandtobago','tt'), 'TT'),
(('tunisia','tn'), 'TN'),
(('turkey','tr'), 'TR'),
(('uganda','ug'), 'UG'),
(('ukraine','ua'), 'UA'),
(('unitedarabemirates','ae'), 'AE'),
(('unitedkingdom','gb'), 'GB'),
(('unitedstates', 'usa','us'), 'US'),
(('uruguay','uy'), 'UY'),
(('uzbekistan','uz'), 'UZ'),
(('venezuela','ve'), 'VE'),
(('vietnam','vn'), 'VN'),
(('yemen','ye'), 'YE'),
]
def main():
sitelist = []
page = 0
while len(sitelist) < args.numsites:
if args.country:
r = requests.get(ALEXA_COUNTRY_URL % (page, args.country))
else:
r = requests.get(ALEXA_GLOBAL_URL % page)
for match in re.finditer(r'<a href="/siteinfo/(.*)">', r.text):
sitelist.append(match.group(1))
page += 1
for site in sitelist[:args.numsites]:
print 'http://%s' % site
if __name__ == "__main__":
# set up command line args
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,\
description='Downloads a list of top sites from Alexa.')
parser.add_argument('-n', '--numsites', default=100, help='Number of top sites to retrieve')
parser.add_argument('-c', '--country', help='Restrict results to top sites for specified country. Default is global.')
args = parser.parse_args()
args.numsites = int(args.numsites)
if args.country:
found = False
for names, code in COUNTRIES:
if args.country.lower().replace(' ','') in names:
args.country = code
found = True
break
if not found:
print 'Unrecognized country: %s' % args.country
sys.exit(-1)
main()