【问题标题】:Extract 2nd level domain from domain? - Python从域中提取二级域? - Python
【发布时间】:2011-06-22 11:02:27
【问题描述】:

我有一个域列表,例如

  • 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


有什么想法吗? :)

【问题讨论】:

标签: javascript jquery python html django


【解决方案1】:

没有办法可靠地得到它。子域是任意的,并且每天都有一个庞大的域扩展列表。最好的情况是您检查域名扩展的怪物列表并维护该列表。

列表: http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1

【讨论】:

【解决方案2】:

遵循@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)

【讨论】:

    【解决方案3】:

    使用 python tld

    https://pypi.python.org/pypi/tld

    $ pip install 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'
    

    【讨论】:

      【解决方案4】:

      混合提取 1 级和 2 级的问题。

      简单的解决方案...

      建立可能的网站后缀列表,从小到大排列。 “co.uk”、“uk”、“co.jp”、“jp”、“com”

      并检查,可以在域末尾匹配后缀。如果匹配,则下一部分是网站。

      【讨论】:

        【解决方案5】:

        唯一可能的方法是通过一个包含所有可能顶级域(如 .com 或 co.uk)的列表。然后,您将浏览此列表并结帐。我没有看到任何其他方式,至少在运行时不访问互联网。

        【讨论】:

        • 您需要该列表,即使在运行时访问 Internet。向最终用户出售三级域或二级域的决定由 CCTLD 的权威机构做出。我认为有些人甚至保留了一些二级域名,并在这些域名和其他地方出售二级域名。当然,您还需要维护该列表,因为这些事情确实会发生变化(而且那是在您考虑创建新的 CCTLD 之前)
        • 谢谢!知道我可以在哪里获取列表吗?感觉像是不可能完成的任务:S
        【解决方案6】:

        @休博斯韦尔

        在您的示例中,您处理的不是特殊域,例如议会.uk,它们在文件中用“!”表示(例如!parliament.uk)

        我对你的代码做了一些修改,也让它看起来更像我以前使用的 PHP 函数。

        还增加了从本地文件加载数据的可能性。

        还用一些域进行了测试,例如:

        • niki.bg,niki.1.bg
        • parliament.uk
        • niki.at、niki.co.at
        • niki.us,niki.ny.us
        • niki.museum, niki.national.museum
        • www.niki.uk - 由于 Mozilla 文件中的“*”,报告为 OK。

        请随时与我联系@github,以便我可以将您添加为那里的共同作者。

        GitHub 仓库在这里:

        https://github.com/nmmmnu/TLDExtractor/blob/master/TLDExtractor.py

        【讨论】:

          猜你喜欢
          • 2014-02-06
          • 1970-01-01
          • 2014-11-14
          • 1970-01-01
          • 2022-11-09
          • 1970-01-01
          • 2017-10-16
          • 1970-01-01
          相关资源
          最近更新 更多