Urllib.parse Common Function Summary of Python3 (Urlencode,quote,quote_plus,unquote,unquote_plus, etc.) _python

Source: Internet
Author: User
Tags urlencode

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.