[英]python requests module request params encoded url is different with the intended url
I'm having a problem with url encoding on a python project requests module.我在 python 项目请求模块上的 url 编码有问题。
These are the two different url encoded params that I got from wireshark packet这些是我从wireshark数据包中获得的两个不同的url编码参数
- 0900+%28%EB%8C%80%ED%95%9C%EB%AF%BC%EA%B5%AD+%ED%91%9C%EC%A4%80%EC%8B%9C%29
0900+%28%EB%8C%80%ED%95%9C%EB%AF%BC%EA%B5%AD+%ED%91%9C%EC%A4%80%EC%8B%9C%29
- 0900%20(%EB%8C%80%ED%95%9C%EB%AF%BC%EA%B5%AD%20%ED%91%9C%EC%A4%80%EC%8B%9C)
0900%20(%EB%8C%80%ED%95%9C%EB%AF%BC%EA%B5%AD%20%ED%91%9C%EC%A4%80%EC%8B%9C)
'1' is the python requests module encoded url and '2' is the url from web browser sent packet. '1' 是 python 请求模块编码 url 和 '2' 是 url 来自 Z2567Z940EC970E061893 的 C 浏览器发送的数据包。 When I decode both of them, it shows the same utf-8 text.
当我解码它们时,它显示相同的 utf-8 文本。
Seems like handling of blank space and parentheses is different between them.似乎它们之间对空格和括号的处理是不同的。 Is there a way I can change '1' to '2'?
有没有办法可以将“1”更改为“2”?
Here are the codes I used to send request这是我用来发送请求的代码
_url = "http://something"
_headers = {
'Accept': 'text/javascript',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'ko-KR',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest'
}
_params = {
'action': 'log',
'datetime': '0900 (대한민국 표준시)'
}
# This is the request part
session = requests.Session()
res = session.get(_url, headers=_headers, params=_params)
You can manually encode your _params
to construct your query string and then concatenate it to your _url
.您可以手动编码您的
_params
以构造您的查询字符串,然后将其连接到您的_url
。
You can use
urllib.parse.urlencode
[Python-Docs] to convert your_params
dictionary to a percent-encoded ASCII text string.您可以使用
urllib.parse.urlencode
[Python-Docs]将您的_params
字典转换为百分比编码的 ASCII 文本字符串。 The resulting string is a series ofkey=value
pairs separated by&
characters, where both key and value are quoted using thequote_via
function.生成的字符串是一系列由
&
字符分隔的key=value
对,其中 key 和 value 都使用quote_via
function 引用。 By default,quote_plus()
is used to quote the values, which means spaces are quoted as a+
character and/
characters are encoded as%2F
, which follows the standard for GET requests (application/x-www-form-urlencoded).默认情况下,
quote_plus()
用于引用值,这意味着空格被引用为+
字符和/
字符被编码为%2F
,这遵循 GET 请求的标准 (application/x-www-form-urlencoded)。 An alternate function that can be passed asquote_via
isquote()
, which will encode spaces as%20
and not encode/
characters.可以作为 quote_via 传递的替代
quote_via
是quote()
,它将空格编码为%20
而不是编码/
字符。 For maximum control of what is quoted, usequote
and specify a value for safe.为了最大限度地控制引用的内容,请使用
quote
并指定安全值。
from urllib.parse import quote_plus, quote, urlencode
import requests
url_template = "http://something/
?{}"
_headers = { ... }
_params = {"action": "log", "datetime": "0900 (대한민국 표준시)"}
_url = url_template.format(urlencode(_params, safe="()", quote_via=quote))
response = requests.get(_url, headers=_headers)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.