Home  >  Article  >  Backend Development  >  Summary of common functions of urllib.parse in Python3

Summary of common functions of urllib.parse in Python3

高洛峰
高洛峰Original
2017-02-22 16:42:161931browse

The examples in this article describe the common functions of urllib.parse in Python3. Share it with everyone for your reference, the details are as follows:

1. Get the 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'
#注意:加号会被解码,可能有时并不是我们想要的
>>> 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': 99,
  }
>>> 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 - give the example again 1Watch it five times.

For more articles related to the summary of common functions of urllib.parse in Python3, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn