解析包含句点的 request.get() params 参数中的值 [英] Parsing values in params argument to request.get() which contain periods
问题描述
我正在使用 python 包请求向服务器发出获取请求.我的目标是复制某些已知的 http 请求并将它们合并到我的 python 脚本中.原始查询字符串包含一个键值对version=0%2E12",我知道它是 url 编码的等价于version=0.12".下面是我正在做的复制 http 请求的示例.
I am using the python package requests to make a get request to a server. My goal is to replicate certain known http requests and incorporated them into my python script. The original query string contains one key-value pair 'version=0%2E12' which I know is the url encoded equivalent of 'version=0.12'. Below is an example of what I am doing to replicate the http request.
params = {
'version'='0.12',
'language'='en',
...
}
resp = requests.get(url, params)
生成的 url 查询字符串中包含 'version=0.12'.如果我尝试将 params 的设置更改为以下内容,
The resulting url query string has 'version=0.12' in it. If I try to change the setting of params to the following,
params = {
'version'='0%2E12',
'language'='en',
...
}
但结果查询字符串包含version=0%252E31".本质上,请求正在解析要进行 url 编码的 %.
but the resulting query string contains 'version=0%252E31'. Essentially, requests is parsing the % to be url encoded.
如何在不手动编写整个 url 的情况下获取正确解析带句点的 params 参数的请求?
How can I get requests to parse my params arguments with periods properly without manually writing the entire url?
推荐答案
请注意 .
是 URL 编码值中完全有效的字符.
Note that .
is a perfectly valid character in a URL-encoded value.
您需要自己对值进行编码,绕过标准编码.我会使用 urllib.urlencode()
,然后用 %2E
手动替换 .
字符.
You'd need to encode the values yourself, bypassing the standard encoding. I'd use urllib.urlencode()
, then replace the .
characters by %2E
by hand.
然后您必须将编码的字符串作为查询参数附加到 URL,并配置 requests
以不重新编码值:
You then have to append the encoded string as a query parameter to the URL, and configure requests
to not re-encode the values:
params = {
'version'='0.12',
'language'='en',
...
}
params = urllib.urlencode(params)
params = params.replace('.', '%2E')
resp = requests.get('?'.join(url, params), config={'encode_uri': False})
设置为 False
的 encode_uri
参数在这里起作用;这是requests
default 否则将重新编码整个 URL 以确保符合 RFC.
The encode_uri
parameter set to False
does the trick here; it's this requests
default that otherwise will re-encode the whole URL to ensure RFC compliancy.
这篇关于解析包含句点的 request.get() params 参数中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!