This article illustrates the common functions of Python3 urllib.parse. Share to everyone for your reference, specific as follows:
1. Get URL parameters
>>> 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 '
#注意: The plus sign will be decoded and may sometimes not be what we want
> >> 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
>>> from Urllib Import Parse
>>> query = {
' name ': ' Walker ',
' age ': $,
}
> >> parse.urlencode (query)
' name=walker&age=99 '
3, Quote/quote_plus
>>> 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
From Urllib Import Parse
>>> parse.unquote (' 1+2 ') #不解码加号
' 1+2 '
>>> Parse.unquote (' 1+2 ') #把加号解码为空格
' 1 2 '
If you still want to ask why there is no urldecode--to see Example 1 again five times. ^_^
More information about Python-related content can be viewed in this site: "Python URL Operation tips Summary", "Python Picture Operation tips Summary", "Python data structure and algorithm tutorial", "Python Socket Programming Skills Summary", " Python function Usage Tips Summary, python string manipulation tips, Python introductory and Advanced classic tutorials, and Python file and directory how-to tips
Hopefully this article will help you with your Python programming.