How to extract hostname from the given URL in Python?

后端 未结 2 1062
臣服心动
臣服心动 2021-01-27 10:43

How can I extract the hostname from: hostname:/file_name ? For example in ngs.pradhi.com:/upload, I want to extract ngs.pradhi.com from it

2条回答
  •  心在旅途
    2021-01-27 11:25

    You should consider using the urlparse module:

    This module defines a standard interface to break Uniform Resource Locator (URL) strings up in components (addressing scheme, network location, path etc.), to combine the components back into a URL string, and to convert a “relative URL” to an absolute URL given a “base URL.”

    Example:

    >>> import urlparse
    >>> urlparse.urlparse('http://ngs.pradhi.com/upload')
    ParseResult(scheme='http', netloc='ngs.pradhi.com', path='/upload', params='', query='', fragment='')
    

提交回复
热议问题