Package: tld
Extract the top level domain (TLD) from the URL given. List of TLD names is
taken fromPublic Suffix.Optionally raises exceptions on non-existing TLDs or silently fails (iffail_silentlyargument is set to True).PrerequisitesPython 3.7, 3.8, 3.9, 3.10 or 3.11.DocumentationDocumentation is available onRead the Docs.InstallationLatest stable version on PyPI:pipinstalltldOr latest stable version from GitHub:pipinstallhttps://github.com/barseghyanartur/tld/archive/stable.tar.gzUsage examplesIn addition to examples below, see thejupyter notebookworkbook file.Get the TLD nameas stringfrom the URL givenfromtldimportget_tldget_tld("http://www.google.co.uk")# 'co.uk'get_tld("http://www.google.idontexist",fail_silently=True)# NoneGet the TLD asan objectfromtldimportget_tldres=get_tld("http://some.subdomain.google.co.uk",as_object=True)res# 'co.uk'res.subdomain# 'some.subdomain'res.domain# 'google'res.tld# 'co.uk'res.fld# 'google.co.uk'res.parsed_url# SplitResult(#     scheme='http',#     netloc='some.subdomain.google.co.uk',#     path='',#     query='',#     fragment=''# )Get TLD name,ignoring the missing protocolfromtldimportget_tld,get_fldget_tld("www.google.co.uk",fix_protocol=True)# 'co.uk'get_fld("www.google.co.uk",fix_protocol=True)# 'google.co.uk'Return TLD parts as tuplefromtldimportparse_tldparse_tld('http://www.google.com')# 'com', 'google', 'www'Get the first level domain nameas stringfrom the URL givenfromtldimportget_fldget_fld("http://www.google.co.uk")# 'google.co.uk'get_fld("http://www.google.idontexist",fail_silently=True)# NoneCheck if some tld is a valid tldfromtldimportis_tldis_tld('co.uk)# Trueis_tld('uk')# Trueis_tld('tld.doesnotexist')# Falseis_tld('www.google.com')# FalseUpdate the list of TLD namesTo update/sync the tld names with the most recent versions run the following
from your terminal:update-tld-namesOr simply do:fromtld.utilsimportupdate_tld_namesupdate_tld_names()Note, that this will update all registered TLD source parsers (not only the
list of TLD names taken from Mozilla). In order to run the update for a single
parser, appenduidof that parser as argument.update-tld-namesmozillaCustom TLD parsersBy default list of TLD names is taken from Mozilla. Parsing implemented in
thetld.utils.MozillaTLDSourceParserclass. If you want to use another
parser, subclass thetld.base.BaseTLDSourceParser, provideuid,source_url,local_pathand implement theget_tld_namesmethod.
Take thetld.utils.MozillaTLDSourceParseras a good example of such
implementation. You could then useget_tld(as well as othertldmodule functions) as shown below:fromtldimportget_tldfromsome.moduleimportCustomTLDSourceParserget_tld("http://www.google.co.uk",parser_class=CustomTLDSourceParser)Custom list of TLD namesYou could maintain your own custom version of the TLD names list (even multiple
ones) and use them simultaneously with built in TLD names list.You would then store them locally and provide a path to it as shown below:fromtldimportget_tldfromtld.utilsimportBaseMozillaTLDSourceParserclassCustomBaseMozillaTLDSourceParser(BaseMozillaTLDSourceParser):uid:str='custom_mozilla'local_path:str='tests/res/effective_tld_names_custom.dat.txt'get_tld("http://www.foreverchild",parser_class=CustomBaseMozillaTLDSourceParser)# 'foreverchild'Same goes for first level domain names:fromtldimportget_fldget_fld("http://www.foreverchild",parser_class=CustomBaseMozillaTLDSourceParser)# 'www.foreverchild'Note, that in both examples shown above, there the original TLD names file has
been modified in the following way:...
// ===BEGIN ICANN DOMAINS===

// This one actually does not exist, added for testing purposes
foreverchild
...Free up resourcesTo free up memory occupied by loading of custom TLD names, usereset_tld_namesfunction withtld_names_local_pathparameter.fromtldimportget_tld,reset_tld_names# Get TLD from a custom TLD names parserget_tld("http://www.foreverchild",parser_class=CustomBaseMozillaTLDSourceParser)# Free resources occupied by the custom TLD names listreset_tld_names("tests/res/effective_tld_names_custom.dat.txt")TroubleshootingIf somehow domain names listedhereare not recognised, make sure you have the most recent version of TLD names in
your virtual environment:update-tld-namesTo update TLD names list for a single parser, specify it as an argument:update-tld-namesmozillaTestingSimply type:pytestOr use tox:toxOr use tox to check specific env:tox-epy39Writing documentationKeep the following hierarchy.=====
title
=====

header
======

sub-header
----------

sub-sub-header
~~~~~~~~~~~~~~

sub-sub-sub-header
^^^^^^^^^^^^^^^^^^

sub-sub-sub-sub-header
++++++++++++++++++++++

sub-sub-sub-sub-sub-header
**************************LicenseMPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-laterSupportFor security issues contact me at the e-mail given in theAuthorsection.For overall issues, go toGitHub.AuthorArtur Barseghyan <artur.barseghyan@gmail.com>