Parsing hostname and port from string or url

前端 未结 5 2145
迷失自我
迷失自我 2021-02-07 00:54

I can be given a string in any of these formats:

  • url: e.g http://www.acme.com:456

  • string: e.g www.acme.com:456, www.acme.com 456, or www.acme.co

5条回答
  •  时光取名叫无心
    2021-02-07 01:25

    Method using urllib -

        from urllib.parse import urlparse
        url = 'https://stackoverflow.com/questions'
        print(urlparse(url))
    

    Output -

    ParseResult(scheme='https', netloc='stackoverflow.com', path='/questions', params='', query='', fragment='')

    Reference - https://www.tutorialspoint.com/urllib-parse-parse-urls-into-components-in-python

提交回复
热议问题