且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从URL获取协议+主机名

更新时间:2023-02-23 18:01:56

您应该可以使用 urlparse (文档: python2 python3 ):

You should be able to do it with urlparse (docs: python2, python3):

from urllib.parse import urlparse
# from urlparse import urlparse  # Python 2
parsed_uri = urlparse('http://***.com/questions/1234567/blah-blah-blah-blah' )
result = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri)
print(result)

# gives
'http://***.com/'