Rad*_*Hex 7 html javascript python django jquery
我有一个域列表,例如
site.co.uk
site.com
site.me.uk
site.jpn.com
site.org.uk
site.it
域名也可以包含第3和第4级域名,例如
test.example.site.org.uk
test2.site.com
在所有这些情况下,我需要尝试提取二级域名 site
有任何想法吗?:)
无法可靠地得到它.子域是任意的,并且每天都有一个域扩展的怪物列表.最好的情况是你检查域扩展的怪物列表并维护列表.
列表:http: //mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1
根据@ kohlehydrat的建议:
import urllib2
class TldMatcher(object):
# use class vars for lazy loading
MASTERURL = "http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1"
TLDS = None
@classmethod
def loadTlds(cls, url=None):
url = url or cls.MASTERURL
# grab master list
lines = urllib2.urlopen(url).readlines()
# strip comments and blank lines
lines = [ln for ln in (ln.strip() for ln in lines) if len(ln) and ln[:2]!='//']
cls.TLDS = set(lines)
def __init__(self):
if TldMatcher.TLDS is None:
TldMatcher.loadTlds()
def getTld(self, url):
best_match = None
chunks = url.split('.')
for start in range(len(chunks)-1, -1, -1):
test = '.'.join(chunks[start:])
startest = '.'.join(['*']+chunks[start+1:])
if test in TldMatcher.TLDS or startest in TldMatcher.TLDS:
best_match = test
return best_match
def get2ld(self, url):
urls = url.split('.')
tlds = self.getTld(url).split('.')
return urls[-1 - len(tlds)]
def test_TldMatcher():
matcher = TldMatcher()
test_urls = [
'site.co.uk',
'site.com',
'site.me.uk',
'site.jpn.com',
'site.org.uk',
'site.it'
]
errors = 0
for u in test_urls:
res = matcher.get2ld(u)
if res != 'site':
print "Error: found '{0}', should be 'site'".format(res)
errors += 1
if errors==0:
print "Passed!"
return (errors==0)
Run Code Online (Sandbox Code Playgroud)
使用 python tld
https://pypi.python.org/pypi/tld
$ pip 安装 tld
from tld import get_tld, get_fld
print(get_tld("http://www.google.co.uk"))
'co.uk'
print(get_fld("http://www.google.co.uk"))
'google.co.uk'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2582 次 |
| 最近记录: |