[PYTHON] Precautions when using the urllib.parse.quote function

The urllib.parse.quote function (urllib.quote function in Python2) used for URL encoding does not encode the path delimiter (/) by default.

>>> help(urllib.parse.quote)

#The character string specified by the argument safe is excluded from encoding.
quote(string, safe='/', encoding=None, errors=None)
    quote('abc def') -> 'abc%20def'
    
(…)

    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.

Therefore, when encoding a character string including a URL with Twitter API etc., it is necessary to set the second argument (or named argument safe) to '' (empty string).

quote_safe


#Path delimiter(/)Is not encoded
>>> urllib.parse.quote('http://hoge.com/api')
'http%3A//hoge.com/api'

#Path delimiter(/)Is encoded
>>> urllib.parse.quote('http://hoge.com/api', '')
'http%3A%2F%2Fhoge.com%2Fapi'
>>> urllib.parse.quote('http://hoge.com/api', safe='')
'http%3A%2F%2Fhoge.com%2Fapi'

Recommended Posts

Precautions when using the urllib.parse.quote function
Precautions when using Chainer
Precautions when using pit in Python
Precautions when inheriting the DatasetMixin class
Precautions when using TextBlob trait analysis
Precautions when using codecs and pandas
Precautions when using phantomjs from python
Precautions when using six with Python 2.5
Precautions when using a list or dictionary as the default argument
Precautions when pickling a function in python
Precautions when using for statements in pandas
Precautions when using OpenCV from Power Automate Desktop
Precautions when using tf.keras.layers.TimeDistributed for tf.keras custom layer
Precautions when installing packages in the conda environment
Precautions when using google-cloud library with GAE / py
Summary when using Fabric
Precautions when handling Luigi
Precautions when using sqlite3 on macOS Sierra (10.12) with multiprocessing
Initial settings when using the foursquare API in python
Function fitting (using Keras)
The first GOLD "Function"
Drawing on Jupyter using the plot function of pandas
I tried to approximate the sin function using chainer
About the Unfold function
Specifies the function to execute when the python program ends
[Django Rest Framework] Customize the filter function using Django-Filter
Precautions when installing fbprophet
Solve the Japanese problem when using the CSV module in Python.
I tried to approximate the sin function using chainer (re-challenge)
(Personal) points when using ctypes
Environment variables when using Tkinter
Precautions when upgrading TensorFlow (to 1.3)
When using optparse with iPython
Regarding the activation function Gelu
Try function optimization using Hyperopt
What is the activation function?
Clone using the dd command
Precautions when drawing the probability density function and the histogram on top of each other in matplotlib
About the enumerate function (python)
DEBUG settings when using Django
Try using the Twitter API
Try using the PeeringDB 2.0 API
When using if and when using while
File structure when using serverless-python-requirements
Use configparser when using API
Outline the face using Dlib (1)
What is the Callback function?
Small speedup when using pytorch
Understand the function of convolution using image processing as an example
Precautions when creating a two-dimensional array with all the same values
The story that a hash error came out when using Pipenv
When you want to save the result of the callback function somewhere
[Linux / GCP] Corrected the error that occurred when using the Git command.
Amazon Rekognition Filter function when registering faces ・ Limiting the number of faces
Finding the optimum value of a function using a genetic algorithm (Part 1)
Precautions when giving default values to arguments in Python function definitions
Try to solve the function minimization problem using particle swarm optimization