用法:
urllib.parse.urlparse(urlstring, scheme='', allow_fragments=True)
將 URL 解析為六個組件,返回一個名為 tuple 的 6 項。這對應於 URL 的一般結構:
scheme://netloc/path;parameters?query#fragment
。每個元組項都是一個字符串,可能為空。組件不會被分解成更小的部分(例如,網絡位置是單個字符串),並且不會擴展 % 轉義。上麵顯示的分隔符不是結果的一部分,除了path
組件中的前導斜杠,如果存在則保留。例如:>>> from urllib.parse import urlparse >>> urlparse("scheme://netloc/path;parameters?query#fragment") ParseResult(scheme='scheme', netloc='netloc', path='/path;parameters', params='', query='query', fragment='fragment') >>> o = urlparse("http://docs.python.org:80/3/library/urllib.parse.html?" ... "highlight=params#url-parsing") >>> o ParseResult(scheme='http', netloc='docs.python.org:80', path='/3/library/urllib.parse.html', params='', query='highlight=params', fragment='url-parsing') >>> o.scheme 'http' >>> o.netloc 'docs.python.org:80' >>> o.hostname 'docs.python.org' >>> o.port 80 >>> o._replace(fragment="").geturl() 'http://docs.python.org:80/3/library/urllib.parse.html?highlight=params'
遵循語法規範RFC 1808, urlparse 僅當它被“//”正確引入時才識別 netloc。否則,輸入被假定為相對 URL,因此以路徑組件開始。
>>> from urllib.parse import urlparse >>> urlparse('//www.cwi.nl:80/%7Eguido/Python.html') ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html', params='', query='', fragment='') >>> urlparse('www.cwi.nl/%7Eguido/Python.html') ParseResult(scheme='', netloc='', path='www.cwi.nl/%7Eguido/Python.html', params='', query='', fragment='') >>> urlparse('help/Python.html') ParseResult(scheme='', netloc='', path='help/Python.html', params='', query='', fragment='')
scheme
參數給出默認尋址方案,僅在 URL 未指定時使用。它應該是與urlstring
相同的類型(文本或字節),除了始終允許使用默認值''
並在適當的情況下自動轉換為b''
。如果
allow_fragments
參數為 false,則無法識別片段標識符。相反,它們被解析為路徑、參數或查詢組件的一部分,並且fragment
在返回值中設置為空字符串。返回值是一個命名元組,這意味著它的項可以通過索引或命名屬性來訪問,它們是:
屬性
index
值
不存在時的值
scheme
0
URL 方案說明符
scheme
參數netloc
1
網絡定位部分
空字符串
path
2
分層路徑
空字符串
params
3
不再使用
總是一個空字符串
query
4
查詢組件
空字符串
fragment
5
片段標識符
空字符串
username
用戶名
password
Password
hostname
主機名(小寫)
port
端口號為整數(如果存在)
如果在 URL 中指定了無效端口,則讀取
port
屬性將引發ValueError
。有關結果對象的更多信息,請參閱結構化解析結果部分。netloc
屬性中不匹配的方括號將引發ValueError
。netloc
屬性中的字符在 NFKC 規範化(由 IDNA 編碼使用)下分解為/
、?
、#
、@
或:
中的任何一個將引發ValueError
。如果在解析之前對 URL 進行了分解,則不會引發錯誤。與所有命名元組的情況一樣,子類具有一些特別有用的附加方法和屬性。一種這樣的方法是
_replace()
。_replace()
方法將返回一個新的 ParseResult 對象,用新值替換指定的字段。>>> from urllib.parse import urlparse >>> u = urlparse('//www.cwi.nl:80/%7Eguido/Python.html') >>> u ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html', params='', query='', fragment='') >>> u._replace(scheme='http') ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html', params='', query='', fragment='')
在 3.2 版中更改:添加了 IPv6 URL 解析函數。
在 3.3 版中更改:現在為所有 URL 方案解析片段(除非
allow_fragment
是假的),按照RFC 3986.以前,存在支持片段的方案的許可列表。在 3.6 版中更改:超出範圍的端口號現在會增加
ValueError
, 而不是返回None
.在 3.8 版中更改:在 NFKC 規範化下影響 netloc 解析的字符現在將引發
ValueError
.
相關用法
- Python urllib.parse.urljoin用法及代碼示例
- Python urllib.parse.urllib.parse.SplitResult.geturl用法及代碼示例
- Python urllib.request.urlretrieve用法及代碼示例
- Python unittest.mock.AsyncMock.assert_awaited_once_with用法及代碼示例
- Python unittest.TestCase.assertWarnsRegex用法及代碼示例
- Python unittest.mock.Mock.reset_mock用法及代碼示例
- Python unittest.mock.Mock.__class__用法及代碼示例
- Python unittest.mock.Mock.call_args用法及代碼示例
- Python unittest.TestCase.assertRaisesRegex用法及代碼示例
- Python unittest.mock.call用法及代碼示例
- Python unittest.mock.Mock.method_calls用法及代碼示例
- Python unittest.mock.Mock.call_args_list用法及代碼示例
- Python unittest.mock.AsyncMock.assert_any_await用法及代碼示例
- Python unittest.mock.Mock.assert_called用法及代碼示例
- Python unittest.TestCase.assertRaises用法及代碼示例
- Python unittest.TestCase.tearDownClass用法及代碼示例
- Python unittest.mock.Mock.assert_not_called用法及代碼示例
- Python unittest.IsolatedAsyncioTestCase用法及代碼示例
- Python unittest.TestCase.setUpClass用法及代碼示例
- Python unittest.mock.AsyncMock.await_args_list用法及代碼示例
注:本文由純淨天空篩選整理自python.org大神的英文原創作品 urllib.parse.urlparse。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。