SoFunction
Updated on 2024-11-17

python3 url encoding and decoding, customize gbk, utf-8 example

Because a lot of time to be involved in the url encoding and decoding work, so they made a class, not much to say on the code see!

# coding:utf-8
import 
 
 
class Urlchuli():
  """Urlprocessing class,Two real parameters need to be passed in:Urlchuli('real parameter','Code type'),default (setting)utf-8  
  urlcoding method:url_bm() urldecoding method:url_jm()"""
 
  def __init__(self,can,mazhi='utf-8'):
     = can
     = mazhi
 
  def url_bm(self):
    """url_bm() Converts incoming Chinese real parameters intoUrlencodeencodings"""
    quma = str().encode()
    bianma = (quma)
    return bianma
 
  def url_jm(self):
    """url_jm() switches the incomingurlDecode to Chinese"""
    quma = str()
    jiema = (quma,)
    return jiema

The url decode call method:

# coding:utf-8
 
from urlbm import Urlchuli
 
a = Urlchuli('%B2%E2%CA%D4%CA%C7%B7%F1%B3%C9%B9%A6','gbk') # The first is the incoming real parameter, the second is the type of url to be decoded, which can be utf-8, gbk or other
print(a.url_jm()) # Print the result: test whether the success (here need to pay attention to, if it is utf-8 will appear messy code)
urlEncoded Call Methods:

# coding:utf-8
 
from urlbm import Urlchuli
 
a = Urlchuli('Tested for success','gbk') # The first is the incoming real parameter, the second is the type of the optional url encoding (default utf-8), which can be utf-8, gbk or other
print(a.url_bm()) # Printed result: %B2%E2%CA%D4%CA%C7%B7%F1%B3%C9%B9%A6

Above this python3 url encoding and decoding, customize gbk, utf-8 example is all I share with you, I hope to give you a reference, and I hope you support me more.