Why Python Requests Use % Is Decode %25 ? Help ME!

I will going to crawl url data about Python requests,
But in use I found a problem,Makes me feel embarrassed。
Why Python requests use % is decode %25 ?
I do not understand……I am a new programmer
Can anyone help tell me the solution to this problem?Thank you again!
Please help directly give the solution to the problem, or the code。
Of course, if it’s just a theory, I still appreciate your help!
(The above article is from Google Translate,I come from china)

For example:

Software instructions:
python 3.9
requests 2.24.0 2.24.0

%25 is the URL-encoded (or percent-encoded) version of a literal %. In query strings this encoding is used for any character that otherwise has special meaning in a URL, or is not an ASCII character.

You can find more details on Wikipedia, there is also a Chinese version of the article:

Also, requests has an easier way to include parameters in the URL:

>>> r = requests.get('https://httpbin.org/get', params={'keyword': '🙂'})
>>> print(r.url)
https://httpbin.org/get?keyword=%F0%9F%99%82

This way you can use any characters in the string, and requests will do the encoding for you. :slightly_smiling_face:

For details, see:

Thanks!But I did not solve this problem……

What i think is this:
The URL I visited contains a % sign,
But I use requests.post,python will add 25 after the % in my website。

For example:
Website goal:http://my.yue365.com/ajax/Search.ashxkeyword=%u6211%u7231%u4F60&Page=1&type=song

After requests,website URL becomes:
http://my.yue365.com/ajax/Search.ashxkeyword=%25u6211**%25u7231%25**u4F60&Page=1&type=song

why???
How can I keep % from being replaced with %25 ???
I want after using the Python requests.post,The symbols and letters in the URL will not change!
what should I do?

Thanks!Thanks!Thanks~

Because %u is not valid in a URL. The % must not occur outside URL encoding.

It looks like you are trying to encode unicode, with %u followed by hexadecimal numbers, but that is not valid in URLs. I’ve tried to decode the characters you’re using with Python:

>>> s = '\u6211\u7231\u4F60'
>>> s
'我爱你'

Now I can use requests to make a request with correct URL encoding:

>>> r = requests.get('https://httpbin.org/get', params={'keyword': '我爱你'})
>>> r.url
'https://httpbin.org/get?keyword=%E6%88%91%E7%88%B1%E4%BD%A0'

The response JSON data shows the characters arrived correctly:

>>> r.json()
{'args': {'keyword': '我爱你'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.22.0', 'X-Amzn-Trace-Id': 'Root=1-5f8726e0-6579f92c60b51d3400fa9dc6'}, 'origin': '85.22.119.60', 'url': 'https://httpbin.org/get?keyword=我爱你'}

If the server does not understand that request, the server is broken.

That said, if you really need those characters unmodified, look at “Prepared Requests” in the requests documentation.

Thanks again for your answer!
But this web-url keyword must be “%u6211%u7231%u4F60”!
If I use “keyword=%E6%88%91%E7%88%B1%E4%BD%A0’” to access the URL,
I will not get the correct json data!
So, please help to see if there are other ways to solve this problem……

I just want to know:
How can I prevent% from being decoded into %25?
(怎样才能让%在编码的时候不出现%25)

I thought it should work with prepared requests, but apparently it does not. If you’re willing to use http.client instead of requests something like this should work:

import http.client
import json
conn = http.client.HTTPSConnection('httpbin.org')
conn.request('POST', '/post?keyword=%u6211%u7231%u4F60')
r = conn.getresponse()
b = r.read()
j = json.loads(b.decode())

j then contains the JSON response. Less convenient, but it works.