Docs.python.org/zh-cn/3.8/l…
Python3 incorporates URllib2, urlparse, and RobotParse into the URllib module. So the way you import in Python should be imported in Python3 like this:
from urllib.parse import urlparse
Copy the code
We can use it to get the ParseResult object, which we can access by subscript or property name:
- Scheme (Protocol)
- Netloc (Domain name)
- Path (path)
- Params (optional)
- Query (join key-value pairs)
- Fragment (Special Anchor)
The property name | The index value | instructions | In the null case |
---|---|---|---|
scheme | 0 | URL agreement | "" |
netloc | 1 | The URL of the domain name | "" |
path | 2 | URL request path | "" |
params | 3 | URL Specifies the URL | "" |
query | 4 | URL key-value pair parameter | "" |
fragment | 5 | Special anchor URL | "" |
username | The user name | None |
|
password | password | None |
|
hostname | Host name (lowercase) | None |
|
port | If it exists, it is an integer port number | None |
|
Let’s test the use of this function: | |||
“`python | |||
#! /usr/bin/env python | |||
# * Coding: UTF-8 * | |||
from urllib.parse import urlparse |
Result = urlparse (‘ juejin. Cn/user / 280560… ‘) print(result)
The output is: ```python ParseResult( scheme='https', netloc='juejin.im', path='/user/5da32395e51d4578200cc9c5/posts', params='', query='params=123&username=123', fragment='' )Copy the code