iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python3的URL编码解码
  • 469
分享到

Python3的URL编码解码

URL 2023-01-31 02:01:22 469人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

博主最近在用python3比较强大的Django开发WEB的时候,发现一些url的编码问题,在浏览器提交请求api时,如果url中包含汉子,就会被自动编码掉。呈现的结果是 ==> %xx%xx%xx。如果出现3个百分号为一个原字符则

博主最近在用python3比较强大的Django开发WEB的时候,发现一些url的编码问题,在浏览器提交请求api时,如果url中包含汉子,就会被自动编码掉。呈现的结果是 ==> %xx%xx%xx。如果出现3个百分号为一个原字符则为utf8编码,如果2个百分号则为gb2312编码。下面为大家演示编码和解码的代码。

from urllib.parse import quote
text = quote(text, 'utf-8')

注:text为要进行编码的字符串

from urllib.parse import unquote
text = unquote(text, 'utf-8')
def unquote(string, encoding='utf-8', errors='replace'):
    """Replace %xx escapes by their single-character equivalent. The optional
    encoding and errors parameters specify how to decode percent-encoded
    sequences into Unicode characters, as accepted by the bytes.decode()
    method.
    By default, percent-encoded sequences are decoded with UTF-8, and invalid
    sequences are replaced by a placeholder character.

    unquote('abc%20def') -> 'abc def'.
    """
    if '%' not in string:
        string.split
        return string
    if encoding is None:
        encoding = 'utf-8'
    if errors is None:
        errors = 'replace'
    bits = _asciire.split(string)
    res = [bits[0]]
    append = res.append
    for i in range(1, len(bits), 2):
        append(unquote_to_bytes(bits[i]).decode(encoding, errors))
        append(bits[i + 1])
    return ''.join(res)


def quote(string, safe='/', encoding=None, errors=None):
    """quote('abc def') -> 'abc%20def'

    Each part of a URL, e.g. the path info, the query, etc., has a
    different set of reserved characters that must be quoted.

    RFC 2396 UnifORM Resource Identifiers (URI): Generic Syntax lists
    the following reserved characters.

    reserved    = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
                  "$" | ","

    Each of these characters is reserved in some component of a URL,
    but not necessarily in all of them.

    By default, the quote function is intended for quoting the path
    section of a URL.  Thus, it will not encode '/'.  This character
    is reserved, but in typical usage the quote function is being
    called on a path where the existing slash characters are used as
    reserved characters.

    string and safe may be either str or bytes objects. encoding and errors
    must not be specified if string is a bytes object.

    The optional encoding and errors parameters specify how to deal with
    non-ASCII characters, as accepted by the str.encode method.
    By default, encoding='utf-8' (characters are encoded with UTF-8), and
    errors='strict' (unsupported characters raise a UnicodeEncodeError).
    """
    if isinstance(string, str):
        if not string:
            return string
        if encoding is None:
            encoding = 'utf-8'
        if errors is None:
            errors = 'strict'
        string = string.encode(encoding, errors)
    else:
        if encoding is not None:
            raise TypeError("quote() doesn't support 'encoding' for bytes")
        if errors is not None:
            raise TypeError("quote() doesn't support 'errors' for bytes")
    return quote_from_bytes(string, safe)

--结束END--

本文标题: Python3的URL编码解码

本文链接: https://www.lsjlt.com/news/185408.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python3的URL编码解码
    博主最近在用python3比较强大的Django开发web的时候,发现一些url的编码问题,在浏览器提交请求api时,如果url中包含汉子,就会被自动编码掉。呈现的结果是 ==> %xx%xx%xx。如果出现3个百分号为一个原字符则...
    99+
    2023-01-31
    URL
  • python3的url编码和解码,自定义
    因为很多时候要涉及到url的编码和解码工作,所以自己制作了一个类,废话不多说 码上见!# coding:utf-8 import urllib.parse class Urlchuli(): """Url处理类,需要传入两个实...
    99+
    2023-01-31
    自定义 url
  • Python3 url解码与参数解析
    在获取zk节点时,有些子节点名字直接就是编码后的url,就像下面这行一样: url='dubbo%3A%2F%2F10.4.5.3%3A20880%2Fcom.welab.authority.service.AuthorityService...
    99+
    2023-01-31
    参数 url
  • Python URL编解码 encode
    urllib包中parse模块的quote和unquote from urllib import parse #这个是js的结果 # encodeURIComponent('中国') # "%E4%B8%AD%E5%9B%BD...
    99+
    2023-01-31
    编解码 Python URL
  • 使用python3的base64编解码实
    把写内容过程中常用的内容段记录起来,下面的资料是关于使用python3的base64编解码实现字符串的简易加密解密的内容。 import base64 copyright = 'Copyright (c) 2012 Doucub...
    99+
    2023-01-31
    编解码
  • Java结合JS实现URL编码与解码
    通常如果一样东西需要编码,说明这样东西并不适合传输。原因多种多样,如Size过大,包含隐私数据,对于Url来说,之所以要进行编码,是因为Url中有些字符会引起歧义。 例如,Url参数...
    99+
    2022-11-13
  • .Net结合JS实现URL编码与解码
    目录解决问题1.为什么需要编码?1.1 浏览器对于中文的编码1.2 需要编码的原因还有几点2.怎样编码?3.实际出现的问题解决方法3.1.escape函数:3.2.encodeURI...
    99+
    2022-11-13
  • JavaScript、C#中URL编码和解码的示例分析
    这篇文章主要为大家展示了“JavaScript、C#中URL编码和解码的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“JavaScript、C#中URL...
    99+
    2022-10-19
  • Python3 json模块之编码解码方法讲解
    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它基于ECMAScript的一个子集。 JSON采用完全独立于语言的文本格式,这些特性使J...
    99+
    2022-11-12
  • Python3的编码问题
    ​介绍Python3中的编码问题前,第一个段落对字节、ASCII​与Unicode与UTF-8等进行基本介绍,如果不对这几种编码犯头晕,可直接跳过。 ASCII​与Unicode与UTF-8与GBK 首先从老大哥说起。跟很多人一样,...
    99+
    2023-01-31
  • 关于 Python3 的编码
    Python3 中 str 与 bytes 的转换:The bytes/str dichotomy in Python 3字符与 Unicode 编号之间的转换# 字符转 Unicode 编号 >>> ord('...
    99+
    2023-01-31
  • js中如何对url进行编码和解码
    目录js 对url进行编码和解码三种编码和解码函数js url二次编码和解码问题URL编码解码原理js 对url进行编码和解码 三种编码和解码函数 encodeURI和 decode...
    99+
    2022-11-16
    js url js对url进行编码 js对url进行解码
  • 解决python3 中的np.load编码问题
    由于在Python2 中的默认编码为ASCII,但是在Python3中的默认编码为UTF-8。 问题: 所以在使用np.load(det.npy)的时候会出现错误提示: you m...
    99+
    2022-11-12
  • python3里gbk编码的问题解决
    在python3有关字符串的处理当中,经常会遇到 'gbk' codec can't encode character '\xa0'这个问题,...
    99+
    2022-11-11
  • 【Python3】02、python编码
    一、ASCII、Unicode和UTF-8的区别       因为字符编码的问题而苦恼不已,于是阅读了大量的博客,再进行了一定的测试,基本搞清楚了编码问题的前因后果。1、字符集和字符编码      计算机中储存的信息都是用二进制数表示的;而...
    99+
    2023-01-31
    python
  • Python3 字符编码
    原文出处:http://www.cnblogs.com/284628487a/p/5584714.html编码字符串是一种数据类型,但是,字符串比较特殊的是还有一个编码问题。因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才...
    99+
    2023-01-31
    字符
  • Java对URL进行编码和解码的两种方法
    使用java.net.URLEncoder和java.net.URLDecoder类 public class UrlEncoder { public static void main(Stri...
    99+
    2023-09-06
    java jvm 开发语言
  • Python3内置json模块编码解码方法详解
    目录JSON简介dumps编码编码字典编码列表编码字符串格式化输出JSON转换关系对照表loads解码总结JSON简介 JSON(JavaScript Object Notation...
    99+
    2022-11-12
  • js对url进行编码解码的三种方式总结
    目录第一种:escape 和 unescape第二种:encodeURI 和 decodeURI第三种:encodeURIComponent 和 decodeURIComponent...
    99+
    2023-02-14
    js url编码解码 url编码和解码 js转码和解码
  • PYTHON3编码再探究
    原文请戳本文大概需要10分钟看完1 看一段简单代码要求:Linux编辑器,python3版本vim test1.py# test1.py内容:import sys, locales = "王佳"print(s)print(sys.getde...
    99+
    2023-06-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作