tldparse


Nametldparse JSON
Version 1.0.5 PyPI version JSON
download
home_pagehttps://github.com/cnicodeme/tldparse
SummaryParse a given domain and split it into its subdomain, domain and top-level domain parts.
upload_time2024-10-25 09:00:32
maintainerNone
docs_urlNone
authorCyril Nicodeme
requires_pythonNone
licenseMIT
keywords domain parser tld top-level domain subdomain
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # TLDParse

Split a given domain into its different parts (subdomain, domain, suffix).

It was built in the aim to be very fast. To do so, it starts in reverse, going from the TLD down to any subdomains.

## Installation

```python
pip install tldparse
```

## Basic usage

The following code will show you the basic usage you can have from this library.

```python
from tldparse import DomainResult

parsed = DomainResult('https://example.github.io/with/some/path')
print(parsed.tld) # io
print(parsed.domain) # github
print(parsed.subdomain) # example
```

## Common usage

A common usage is to check whereas the given domain is the fqdn or if it has subdomains

```python
from tldparse import DomainResult

email = 'user@sub.domain.com'
mbox, domain = email.split('@')

parsed = DomainResult(domain)
assert parsed.fqdn == domain, "Please register using a domain with no subdomain parts"
```

## Advanced usage

### Replacing TLDParse with a custom one:

`DomainResult` class use the default `TLDParse` object that relies on the list of suffixes provided by Mozilla.
(The list is freely available at https://publicsuffix.org/list/public_suffix_list.dat)

But you can either pass another parser, such as:

```python

class MyCustomParser:
    def __call__(self, domain, private=True):
        # PLEASE DON'T DO THAT
        return domain.rsplit('.', 2)  # Consider a TLD has only one part. Ignores things such as ".co.uk"

from tldparse import DomainResult

parser = MyCostumParser()

result = DomainResult('example.github.com', parser=parser)
print(result.fqdn)  # github.com

result = DomainResult('bbc.co.uk', parser=parser)
print(result.fqdn)  # co.uk  # Too bad
```

### Using private subdomains:

By default, the library will consider the subdomains as part of the fqdn for certain domain.
But you can change this behavior by setting the `private` parameter to `True`.

```python
from tldparse import DomainResult

result = DomainResult('example.github.io')
print(result.fqdn) # example.github.io

result = DomainResult('example.github.io', private=False)
print(result.fqdn) # github.io
```

### Adding/Removing TLDs entries:

You can add/remove entries to the parser by using the `add`/`remove` methods of the tldparse object:

```python
from .tldparse import TLDParse, DomainResult

# Adding a custom domain
parser.add('weebly.co.uk')  # added in private
result = DomainResult('example.weebly.co.uk', parser=parser, private=False)
print(result.suffix)  # 'co.uk'
print(result.domain)  # 'weebly'
print(result.subdomain)  # 'example'

result = DomainResult('example.weebly.co.uk', parser=parser)
print(result.suffix)  # 'weebly.co.uk'
print(result.domain)  # 'example'

# Removing a domain
TLDParse.remove('github.io')
result = DomainResult('example.github.io')
print(result.suffix)  # 'io'
print(result.domain)  # 'github'
print(result.subdomain)  # 'example'
```

## Support

If you find an issue, please open a support requests, or even better, a Pull Requests :)

Thank you!

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/cnicodeme/tldparse",
    "name": "tldparse",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "domain parser tld top-level domain subdomain",
    "author": "Cyril Nicodeme",
    "author_email": "contact@cnicodeme.com",
    "download_url": "https://files.pythonhosted.org/packages/bb/d3/23776af21d592a1de87fc3b78dc7b79a74180e91888112f6fd9234f03797/tldparse-1.0.5.tar.gz",
    "platform": "any",
    "description": "# TLDParse\n\nSplit a given domain into its different parts (subdomain, domain, suffix).\n\nIt was built in the aim to be very fast. To do so, it starts in reverse, going from the TLD down to any subdomains.\n\n## Installation\n\n```python\npip install tldparse\n```\n\n## Basic usage\n\nThe following code will show you the basic usage you can have from this library.\n\n```python\nfrom tldparse import DomainResult\n\nparsed = DomainResult('https://example.github.io/with/some/path')\nprint(parsed.tld) # io\nprint(parsed.domain) # github\nprint(parsed.subdomain) # example\n```\n\n## Common usage\n\nA common usage is to check whereas the given domain is the fqdn or if it has subdomains\n\n```python\nfrom tldparse import DomainResult\n\nemail = 'user@sub.domain.com'\nmbox, domain = email.split('@')\n\nparsed = DomainResult(domain)\nassert parsed.fqdn == domain, \"Please register using a domain with no subdomain parts\"\n```\n\n## Advanced usage\n\n### Replacing TLDParse with a custom one:\n\n`DomainResult` class use the default `TLDParse` object that relies on the list of suffixes provided by Mozilla.\n(The list is freely available at https://publicsuffix.org/list/public_suffix_list.dat)\n\nBut you can either pass another parser, such as:\n\n```python\n\nclass MyCustomParser:\n    def __call__(self, domain, private=True):\n        # PLEASE DON'T DO THAT\n        return domain.rsplit('.', 2)  # Consider a TLD has only one part. Ignores things such as \".co.uk\"\n\nfrom tldparse import DomainResult\n\nparser = MyCostumParser()\n\nresult = DomainResult('example.github.com', parser=parser)\nprint(result.fqdn)  # github.com\n\nresult = DomainResult('bbc.co.uk', parser=parser)\nprint(result.fqdn)  # co.uk  # Too bad\n```\n\n### Using private subdomains:\n\nBy default, the library will consider the subdomains as part of the fqdn for certain domain.\nBut you can change this behavior by setting the `private` parameter to `True`.\n\n```python\nfrom tldparse import DomainResult\n\nresult = DomainResult('example.github.io')\nprint(result.fqdn) # example.github.io\n\nresult = DomainResult('example.github.io', private=False)\nprint(result.fqdn) # github.io\n```\n\n### Adding/Removing TLDs entries:\n\nYou can add/remove entries to the parser by using the `add`/`remove` methods of the tldparse object:\n\n```python\nfrom .tldparse import TLDParse, DomainResult\n\n# Adding a custom domain\nparser.add('weebly.co.uk')  # added in private\nresult = DomainResult('example.weebly.co.uk', parser=parser, private=False)\nprint(result.suffix)  # 'co.uk'\nprint(result.domain)  # 'weebly'\nprint(result.subdomain)  # 'example'\n\nresult = DomainResult('example.weebly.co.uk', parser=parser)\nprint(result.suffix)  # 'weebly.co.uk'\nprint(result.domain)  # 'example'\n\n# Removing a domain\nTLDParse.remove('github.io')\nresult = DomainResult('example.github.io')\nprint(result.suffix)  # 'io'\nprint(result.domain)  # 'github'\nprint(result.subdomain)  # 'example'\n```\n\n## Support\n\nIf you find an issue, please open a support requests, or even better, a Pull Requests :)\n\nThank you!\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Parse a given domain and split it into its subdomain, domain and top-level domain parts.",
    "version": "1.0.5",
    "project_urls": {
        "Homepage": "https://github.com/cnicodeme/tldparse",
        "Source": "https://github.com/cnicodeme/tldparse"
    },
    "split_keywords": [
        "domain",
        "parser",
        "tld",
        "top-level",
        "domain",
        "subdomain"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fce243b77ccbf36814e65a8111d121ab3831cf1fba33b0ca4564c47b1af8c4db",
                "md5": "10979408301a50c4c37f5d4f97ed718d",
                "sha256": "4758810bf461246807f89da92a90c4120cd3675ee423c2431a0a5cfacd52d9eb"
            },
            "downloads": -1,
            "filename": "tldparse-1.0.5-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "10979408301a50c4c37f5d4f97ed718d",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 91896,
            "upload_time": "2024-10-25T09:00:30",
            "upload_time_iso_8601": "2024-10-25T09:00:30.795681Z",
            "url": "https://files.pythonhosted.org/packages/fc/e2/43b77ccbf36814e65a8111d121ab3831cf1fba33b0ca4564c47b1af8c4db/tldparse-1.0.5-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bbd323776af21d592a1de87fc3b78dc7b79a74180e91888112f6fd9234f03797",
                "md5": "eb1003d001c1fa9d4c5f7b1df61218e8",
                "sha256": "47ed932c0937fec8545ad2b7d89429eea941b9e3301223c363db2d3f8f343412"
            },
            "downloads": -1,
            "filename": "tldparse-1.0.5.tar.gz",
            "has_sig": false,
            "md5_digest": "eb1003d001c1fa9d4c5f7b1df61218e8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 93543,
            "upload_time": "2024-10-25T09:00:32",
            "upload_time_iso_8601": "2024-10-25T09:00:32.858190Z",
            "url": "https://files.pythonhosted.org/packages/bb/d3/23776af21d592a1de87fc3b78dc7b79a74180e91888112f6fd9234f03797/tldparse-1.0.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-25 09:00:32",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "cnicodeme",
    "github_project": "tldparse",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "tldparse"
}
        
Elapsed time: 1.09670s