Python 解析URL及参数的方法(Python2和Python3)

本文主要Python(Python2和Python3)中,解析URL包括其中的参数、端口、协议及路径的方法,以及相关的示例代码。

1、Python2中解析参数、端口、协议及路径

#!/usr/bin/env python
# encoding: utf-8

import urllib
import urlparse


#原文地址:https://www.cjavapy.com/article/2538/
proto, rest = urllib.splittype("http://192.168.31.11:8081/11/12.htm")
print(proto, rest)       # ('http', '//192.168.31.11:8081/11/12.htm')
host, rest = urllib.splithost(rest)
print(host, rest)        # ('192.168.8.24:8161', '/11/12.htm')
host, port = urllib.splitport(host)
if port is None:
    port =80
print(host, port)
url="http://192.168.31.11:8080/artcle/11.html?user=1&token=b7daea86ac024581bf5b8c36cdae3e5d"
param = urlparse.parse_qs(urlparse.urlsplit(url).query)
print("参数:user={0},token={1}".format(param["user"][0],param["token"][0]))
_url = urlparse.urlparse(url)
hostname = _url.hostname
port = _url.port
url_port = _url.netloc
url_path = _url.path
url_protocol = _url.scheme
print('域名:{0}\n端口:{1}\n域名+端口:{2}\n域名路径地址:{3}\n域名协议:{4}'.format(hostname,port,url_port,url_path,url_protocol))

2、Python2中解析参数、端口、协议及路径

import urllib.parse
from urllib.parse import urlparse

#原文地址:https://www.cjavapy.com/article/2538/
"""proto, rest = urllib.parse.splittype("http://192.168.31.11:8081/11/12.htm")
print(proto, rest)       # ('http', '//192.168.31.11:8081/11/12.htm')
host, rest = urllib.parse.splithost(rest)
print(host, rest)        # ('192.168.8.24:8161', '/11/12.htm')
host, port = urllib.parse.splithost(host)
print(urllib.parse.urlparse(host))
if port is None:
    port =80
print(host, port)"""
url="http://192.168.31.11:8080/artcle/11.html?user=1&token=b7daea86ac024581bf5b8c36cdae3e5d"
param = urllib.parse.parse_qs(urllib.parse.urlsplit(url).query)
print("参数:user={0},token={1}".format(param["user"][0],param["token"][0]))
_url = urlparse(url)
hostname = _url.hostname
port = _url.port
url_port = _url.netloc
url_path = _url.path
url_protocol = _url.scheme
print('域名:{0}\n端口:{1}\n域名+端口:{2}\n域名路径地址:{3}\n域名协议:{4}'.format(hostname,port,url_port,url_path,url_protocol))
推荐阅读
cjavapy编程之路首页