天天看點

Python程式設計:urlsplit, urlparse簡單差別代碼示例

顧名思義,urlsplit是拆分,而urlparse是解析,是以urlparse粒度更為細緻

差別

split函數在分割的時候,path和params屬性是在一起的

代碼示例

# -*- coding: utf-8 -*-

from urllib.parse import urlsplit, urlparse

url = "https://username:[email protected]:80/index.html;parameters?name=tom#example"

print(urlsplit(url))
"""
SplitResult(
    scheme='https', 
    netloc='username:[email protected]:80', 
    path='/index.html;parameters', 
    query='name=tom', 
    fragment='example')
"""

print(urlparse(url))
"""
ParseResult(
    scheme='https', 
    netloc='username:[email protected]:80', 
    path='/index.html', 
    params='parameters', 
    query='name=tom', 
    fragment='example'
)
"""      

參考:

Python教程:[20]urlsplit和urlparse的差別