本文实例讲述了python对url格式解析的方法。分享给大家供大家参考。具体分析如下:
python针对url格式的解析,可根据指定的完整URL解析出url地址的各个部分
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
from
urlparse import
urlparse url_str
=
"http://www.163.com/mail/index.htm" 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)] |
希望本文所述对大家的Python程序设计有所帮助。
from urlparse import urlparse
if __name__=="__main__":
#Main().start()
url="https://zhidao.baidu.com/question/421540587.html?fr=iks&word=%C4%E3%BA%C3&ie=gbk"
url = urlparse(url)
print 'protocol:',url.scheme
print 'hostname:',url.hostname
print 'port:',url.port
print 'path:',url.path
print 'query:',url.query #查询参数,格式a=1
protocol: https
hostname: zhidao.baidu.com
port: None
path: /question/421540587.html
query: fr=iks&word=%C4%E3%BA%C3&ie=gbk