Skip to content

Instantly share code, notes, and snippets.

@WinLinux1028
Last active September 5, 2023 15:20
Show Gist options
  • Save WinLinux1028/1e3e3cd8bdfabfb05e40d9b6f685c560 to your computer and use it in GitHub Desktop.
Save WinLinux1028/1e3e3cd8bdfabfb05e40d9b6f685c560 to your computer and use it in GitHub Desktop.
ドメインが書かれたテキストファイルから同じTLDの数を数えるやつ
import sys
from publicsuffix2 import fetch, get_tld
from pprint import pprint
psl_file = fetch()
length = 9000
headers = {
"Content-Type": "application/json",
}
hosts = {}
cnt = 0
with open(sys.argv[1]) as f:
for domain in f:
domain = str.strip(domain)
cnt += 1
tld = get_tld(domain, psl_file=psl_file)
print(f"domain:{domain} tld:{tld}")
if tld in hosts:
hosts[tld] += 1
else:
hosts[tld] = 1
hosts = sorted(hosts.items(), key=lambda x: x[1], reverse=True)
with open("result.csv", "w") as f:
for tld, count in hosts:
f.write(f"{tld},{count}\n")
pprint(hosts)
print(f"total server:{cnt}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment