Extract domain name from a host name

后端 未结 3 786
逝去的感伤
逝去的感伤 2021-02-05 10:34

Is there a programatic way to find the domain name from a given hostname?

given -> www.yahoo.co.jp return -> yahoo.co.jp

The approach that works but is very slo

3条回答
  •  南方客
    南方客 (楼主)
    2021-02-05 11:04

    You can use partition instead of split:

    >>> 'www.yahoo.co.jp'.partition('.')[2]
    'yahoo.co.jp'
    

    This will help with the parsing but obviously won't check if the returned string is a valid domain.

提交回复
热议问题