1、Python2中解析参数、端口、协议及路径
import urllib
import urlparse
proto, rest = urllib.splittype("http://192.168.31.11:8081/11/12.htm")
print(proto, rest)
host, rest = urllib.splithost(rest)
print(host, rest)
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
"""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))