English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python3Summary of Common Functions of urllib.parse (urlencode, quote, quote_plus, unquote, unquote_plus, etc.)

This article describes Python3Common functions of urllib.parse. Shared for everyone's reference, as follows:

1Get URL parameters


>>> url = r'https://docs.python.org/3.5/search.html#63;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'
#Note: Plus signs will be decoded, and sometimes they may 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']}

2urlencode


>>> query = {
  'name': 'walker',
  'age': 99,
  
>>> parse.urlencode(query)
'name=walker&age=99

3/quote_plus


>>> parse.quote('a&b/c')  # Not encoded the slash
'a%26b/c'
>>> parse.quote_plus('a&b/c')  # Encoded the slash
'a%26b%2Fc'

4/unquote_plus

from urllib import parse
>>> parse.unquote('1+2
1+2
>>> parse.unquote('1+2')  # Decode the plus sign to a space
1 2

If you still want to ask why there is no urldecode, then see the example1Read it five times. ^_^

Readers who are interested in more about Python-related content can check the special topics on this site: 'Summary of Python URL Operation Skills', 'Summary of Python Image Operation Skills', 'Python Data Structures and Algorithms Tutorial', 'Summary of Python Socket Programming Skills', 'Summary of Python Function Usage Skills', 'Summary of Python String Operation Skills', 'Classic Tutorial of Python入门与进阶', and 'Summary of Python File and Directory Operation Skills'.

I hope the content described in this article will be helpful to everyone's Python program design.

Declaration: The content of this article is from the Internet, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, does not edit the content manually, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.)

You May Also Like