python url格式解析,分离协议、端口、页面和查询参数

2022-03-11 00:00:00 解析 端口 分离
"""
皮蛋编程(https://www.pidancode.com)
创建日期:2022/4/1
功能描述:python url格式解析,分离协议、端口、页面和查询参数
"""
from urllib.parse import urlparse

url_str = "https://www.pidancode.com/a/16469945464080262.html?test=1"
url = urlparse(url_str)
print('protocol:', url.scheme)
print('hostname:', url.hostname)
print('port:', url.port)
print('path:', url.path)
print('query:', url.query)  # 查询参数,格式a=1
i = len(url.path) - 1
while i > 0:
    if url.path[i] == '/':
        break
    i = i - 1
print('filename:', url.path[i + 1:len(url.path)])

输出如下:
protocol: https
hostname: www.pidancode.com
port: None
path: /a/16469945464080262.html
query: test=1
filename: 16469945464080262.html

以上代码在python3.9环境下测试通过

相关文章