How to extract top-level domain name (TLD) from URL
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: how would you extract the domain name from a URL, excluding any subdomains? My initial simplistic attempt was: '.'.join(urlparse.urlparse(url).netloc.split('.')[-2:]) 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). thanks 回答1: 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