#!/usr/bin/env python ################################################### # google_tld.py -- Quick little script to collect # HTML from each Google TLD # Emanuel Miller # emiller@ccorpsoft.com # # usage: ./google_tld.py ################################################### import time, httplib, sys, os AGENT = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0" HEADERS = {"User-Agent": AGENT, "Connection": "close"} # I'm sure I missed a few, I believe there are 230+ TLDs on the web in total # but these are the ones I could come up with CCTLD = [ "ac", "ad", "ae", "af", "ag", "ai", "al", "am", "an", "ao", "aq", "ar", "as", "at", "au", "aw", "ax", "az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi", "biz", "bj", "bm", "bn", "bo", "br", "bs", "bt", "bv", "bw", "by", "bz", "ca", "cc", "cf", "cg", "ch", "ci", "ck", "cl", "cn", "co", "co.ac", "co.ad", "co.ae", "co.af", "co.ag", "co.ai", "co.al", "co.am", "co.an", "co.ao", "co.aq", "co.ar", "co.as", "co.at", "co.au", "co.aw", "co.ax", "co.az", "co.ba", "co.bb", "co.bd", "co.be", "co.bf", "co.bg", "co.bh", "co.bi", "co.bj", "co.bm", "co.bn", "co.bo", "co.br", "co.bs", "co.bt", "co.bv", "co.bw", "co.by", "co.bz", "co.ca", "co.cc", "co.cd", "co.cf", "co.cg", "co.ch", "co.ci", "co.ck", "co.cl", "co.cn", "co.co", "co.cr", "co.cs", "co.cu", "co.cv", "co.cx", "co.cy", "co.cz", "co.de", "co.dj", "co.dk", "co.dm", "co.do", "co.dz", "co.ec", "co.ee", "co.eg", "co.eh", "co.er", "co.es", "co.et", "co.eu", "co.fi", "co.fj", "co.fk", "co.fm", "co.fo", "co.fr", "co.ga", "co.gb", "co.gd", "co.ge", "co.gf", "co.gg", "co.gh", "co.gi", "co.gl", "co.gm", "co.gn", "co.gp", "co.gq", "co.gr", "co.gs", "co.gt", "co.gu", "co.gw", "co.gy", "co.hk", "co.hm", "co.hn", "co.hr", "co.ht", "co.hu", "co.id", "co.ie", "co.if", "co.il", "co.im", "co.in", "co.io", "co.iq", "co.ir", "co.is", "co.it", "co.je", "co.jm", "co.jo", "co.jp", "co.ke", "co.kg", "co.kh", "co.ki", "co.km", "co.kn", "co.kp", "co.kr", "co.kw", "co.ky", "co.kz", "co.la", "co.lb", "co.lc", "co.li", "co.lk", "co.lr", "co.ls", "co.lt", "co.lu", "co.lv", "co.ly", "co.ma", "co.mc", "co.md", "co.mg", "co.mh", "co.mk", "co.ml", "co.mm", "co.mn", "co.mo", "co.mp", "co.mq", "co.mr", "co.ms", "co.mt", "co.mu", "co.mv", "co.mw", "co.mx", "co.my", "co.mz", "co.na", "co.nc", "co.ne", "co.nf", "co.ng", "co.ni", "co.nl", "co.no", "co.np", "co.nr", "co.nu", "co.nz", "co.om", "co.pa", "co.pe", "co.pf", "co.pg", "co.pk", "co.pl", "co.pm", "co.pn", "co.pr", "co.ps", "co.pw", "co.py", "co.qa", "co.re", "co.ru", "co.rw", "co.sa", "co.sb", "co.sc", "co.sd", "co.se", "co.sg", "co.sh", "co.si", "co.sj", "co.sk", "co.sl", "co.sm", "co.sn", "co.so", "co.sr", "co.st", "co.sv", "co.sy", "co.sz", "co.tc", "co.td", "co.tf", "co.tg", "co.th", "co.tj", "co.tk", "co.tl", "co.tm", "co.tn", "co.to", "co.tp", "co.tr", "co.tt", "co.tv", "co.tw", "co.tz", "co.ua", "co.ug", "co.uk", "co.um", "co.us", "co.uy", "co.uz", "co.va", "co.vc", "co.ve", "co.vg", "co.vi", "co.vn", "co.vu", "co.wf", "co.ye", "co.yt", "co.yu", "co.za", "co.zm", "co.zw", "com", "cr", "cs", "cu", "cv", "cx", "cy", "cz", "de", "dj", "dk", "dm", "do", "dz", "ec", "edu", "ee", "eg", "eh", "er", "es", "et", "eu", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gb", "gd", "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu", "id", "ie", "if", "il", "im", "in", "info", "io", "iq", "ir", "is", "it", "je", "jm", "jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "mg", "mh", "mk", "ml", "mm", "mn", "mo", "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na", "name", "nc", "ne", "net", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om", "org", "pa", "pe", "pf", "pg", "pk", "pl", "pm", "pn", "pr", "ps", "pw", "py", "qa", "re", "ro", "ru", "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so", "sr", "st", "sv", "sy", "sz", "tc", "td", "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn", "to", "tp", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "uk", "um", "us", "uy", "uz", "va", "vc", "ve", "vi", "vn", "vu", "wf", "ye", "yt", "yu", "za", "zm", "zw", ] def returnParsedURL(url): url = url.split("//", 1) if len(url) != 2: return {"domain": "", "path": ""} url_path = url[1].split("/", 1) if len(url_path) == 2: if url_path[1].find("#") != -1: url_path[1] = url_path[1][:url_path[1].find("#")] url_domain = url_path[0] url_path = url_path[1] else: url_domain = url_path[0] url_path = "/" if len(url_path) > 2 and url_path[:2] == "//": url_path = url_path[1:] if url_path[:1] != "/": url_path = "/%s" % url_path return {"domain": url_domain.lower(), "path": url_path} # end returnParsedURL() ############################## def returnDomainHTML(url, headers, depth=0): html_out = "" he = 0 if depth > 5: return html_out while he < 3: try: http_conn = httplib.HTTPConnection(url["domain"]) http_conn.request("GET", url["path"], "", headers) http_data = http_conn.getresponse() if http_data.status == 302 or http_data.status == 301: http_conn.close() loc = http_data.getheader("location") if len(loc) > 5 and loc[:5].lower() != "http:": if loc[:1] == "/": loc = loc[1:] loc = "http://%s/%s" % (url["domain"], loc) return returnDomainHTML(returnParsedURL(loc), headers, (depth+1)) if http_data.status != 200: time.sleep(0.1) he += 1 continue html_out = http_data.read() http_conn.close() he = 0 break except: time.sleep(0.1) he += 1 continue return html_out # end returnDomainHTML() ################################### if len(sys.argv) != 2: print "Usage: %s " % (sys.argv[0]) else: if not os.path.exists(sys.argv[1]): try: os.mkdir(sys.argv[1]) except: print "Could not create output directory: %s" % sys.argv[1] sys.exit() for tld in CCTLD: domain = "www.google.%s" % tld print "Retrieving %s..." % tld html = returnDomainHTML(returnParsedURL("http://%s" % domain), HEADERS) if len(html) != 0: # Some generic hacks, this just fixes some HTML code related to Google html = html.replace("src=\"", "src=\"http://%s/" % domain).replace("src=i", "src=http://%s/i" % domain) html = html.replace("href=\"", "href=\"http://%s" % domain).replace("href=/", "href=http://%s/" % domain) html = html.replace("%s//" % domain, "%s/" % domain) open("%s/%s.html" % (sys.argv[1], tld), "w").write(html)