如何从 URL 中提取顶级域名 (TLD) [英] How to extract top-level domain name (TLD) from URL

查看:40
本文介绍了如何从 URL 中提取顶级域名 (TLD)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 URL 中提取域名,不包括任何子域?

how would you extract the domain name from a URL, excluding any subdomains?

我最初的简单尝试是:

'.'.join(urlparse.urlparse(url).netloc.split('.')[-2:])

这适用于 http://www.foo.com,但不适用于 http://www.foo.com.au.有没有一种方法可以在不使用有关有效 TLD(顶级域)或国家/地区代码(因为它们发生变化)的特殊知识的情况下正确执行此操作.

This works for http://www.foo.com, but not http://www.foo.com.au. Is there a way to do this properly without using special knowledge about valid TLDs (Top Level Domains) or country codes (because they change).

谢谢

推荐答案

不,没有内在"方式知道(例如)zap.co.it 是一个子域(因为意大利的注册商会出售诸如 co.it 之类的域名,而 zap.co.uk 不是(因为英国的注册商不出售域名例如co.uk,但只喜欢zap.co.uk).

No, there is no "intrinsic" way of knowing that (e.g.) zap.co.it is a subdomain (because Italy's registrar DOES sell domains such as co.it) while zap.co.uk isn't (because the UK's registrar DOESN'T sell domains such as co.uk, but only like zap.co.uk).

您只需要使用辅助表(或在线资源)来告诉您哪些 TLD 的行为特别像英国和澳大利亚的 - 没有办法从没有这种额外语义知识(例如当然它最终会改变,但如果你能找到一个好的在线资源,这个资源也会相应地改变,希望!-).

You'll just have to use an auxiliary table (or online source) to tell you which TLD's behave peculiarly like UK's and Australia's -- there's no way of divining that from just staring at the string without such extra semantic knowledge (of course it can change eventually, but if you can find a good online source that source will also change accordingly, one hopes!-).

这篇关于如何从 URL 中提取顶级域名 (TLD)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆