Get subdomain from URL using Python

前端 未结 8 1102
孤城傲影
孤城傲影 2020-12-20 13:00

For example, the address is:

Address = http://lol1.domain.com:8888/some/page

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-20 13:37

    tldextract separate the TLD from the registered domain and subdomains of a URL.

    Installation

    pip install tldextract
    

    For the current question:

    import tldextract
    
    address = 'http://lol1.domain.com:8888/some/page'
    domain = tldextract.extract(address).domain
    print("Extracted domain name : ", domain)
    

    The output:

    Extracted domain name :  domain
    

    In addition, there is a number of examples which is extremely related with the usage of tldextract.extract side.

提交回复
热议问题