各位用户为了找寻关于Python3的urllib.parse常用函数小结(urlencode,quote,quote_plus,unquote,unquote_plus等)的资料费劲了很多周折。这里教程网为您整理了关于Python3的urllib.parse常用函数小结(urlencode,quote,quote_plus,unquote,unquote_plus等)的相关资料,仅供查阅,以下为您介绍关于Python3的urllib.parse常用函数小结(urlencode,quote,quote_plus,unquote,unquote_plus等)的详细内容
本文实例讲述了Python3的urllib.parse常用函数。分享给大家供大家参考,具体如下:
1、获取url参数
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14>>>
from
urllib
import
parse
>>> url
=
r
'https://docs.python.org/3.5/search.html?q=parse&check_keywords=yes&area=default'
>>> parseResult
=
parse.urlparse(url)
>>> parseResult
ParseResult(scheme
=
'https'
, netloc
=
'docs.python.org'
, path
=
'/3.5/search.html'
, params
=
'
', query='
q
=
parse&check_keywords
=
yes&area
=
default
', fragment='
')
>>> param_dict
=
parse.parse_qs(parseResult.query)
>>> param_dict
{
'q'
: [
'parse'
],
'check_keywords'
: [
'yes'
],
'area'
: [
'default'
]}
>>> q
=
param_dict[
'q'
][
0
]
>>> q
'parse'
#注意:加号会被解码,可能有时并不是我们想要的
>>> parse.parse_qs(
'proxy=183.222.102.178:8080&task=XXXXX|5-3+2'
)
{
'proxy'
: [
'183.222.102.178:8080'
],
'task'
: [
'XXXXX|5-3 2'
]}
2、urlencode
? 1 2 3 4 5 6 7>>>
from
urllib
import
parse
>>> query
=
{
'name'
:
'walker'
,
'age'
:
99
,
}
>>> parse.urlencode(query)
'name=walker&age=99'
3、quote/quote_plus
? 1 2 3 4 5>>>
from
urllib
import
parse
>>> parse.quote(
'a&b/c'
)
#未编码斜线
'a%26b/c'
>>> parse.quote_plus(
'a&b/c'
)
#编码了斜线
'a%26b%2Fc'
4、unquote/unquote_plus
? 1 2 3 4 5from
urllib
import
parse
>>> parse.unquote(
'1+2'
)
#不解码加号
'1+2'
>>> parse.unquote(
'1+2'
)
#把加号解码为空格
'1 2'
如果你还想问为什么没有urldecode——再把示例1看五遍。^_^
希望本文所述对大家Python程序设计有所帮助。