如何防止python请求对我的URL进行百分比编码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23496750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to prevent python requests from percent encoding my URLs?
提问by Satyen Rai
I'm trying to GET an URL of the following format using requests.get() in python:
我正在尝试使用 python 中的 requests.get() 获取以下格式的 URL:
http://api.example.com/export/?format=json&key=site:dummy+type:example+group:wheel
http://api.example.com/export/?format=json&key=site:dummy+type:example+group:wheel
#!/usr/local/bin/python
import requests
print(requests.__versiom__)
url = 'http://api.example.com/export/'
payload = {'format': 'json', 'key': 'site:dummy+type:example+group:wheel'}
r = requests.get(url, params=payload)
print(r.url)
However, the URL gets percent encoded and I don't get the expected response.
但是,URL 被百分比编码,我没有得到预期的响应。
2.2.1
http://api.example.com/export/?key=site%3Adummy%2Btype%3Aexample%2Bgroup%3Awheel&format=json
This works if I pass the URL directly:
如果我直接传递 URL,这会起作用:
url = http://api.example.com/export/?format=json&key=site:dummy+type:example+group:wheel
r = requests.get(url)
Is there some way to pass the the parameters in their original form - without percent encoding?
有没有办法以原始形式传递参数 - 没有百分比编码?
Thanks!
谢谢!
采纳答案by furas
It is not good solution but you can use string
:
这不是一个好的解决方案,但您可以使用string
:
r = requests.get(url, params='format=json&key=site:dummy+type:example+group:wheel')
BTW:
顺便提一句:
payload = {'format': 'json', 'key': 'site:dummy+type:example+group:wheel'}
payload_str = "&".join("%s=%s" % (k,v) for k,v in payload.items())
# 'format=json&key=site:dummy+type:example+group:wheel'
r = requests.get(url, params=payload_str)
回答by Kenneth Reitz
The solution, as designed, is to pass the URL directly.
按照设计,解决方案是直接传递 URL。
回答by Brandon McGinty-Carroll
In case someone else comes across this in the future, you can subclass requests.Session, override the send method, and alter the raw url, to fix percent encodings and the like. Corrections to the below are welcome.
如果将来其他人遇到这种情况,您可以将 requests.Session 子类化,覆盖 send 方法,并更改原始 url,以修复百分比编码等。欢迎对以下内容进行更正。
import requests, urllib
class NoQuotedCommasSession(requests.Session):
def send(self, *a, **kw):
# a[0] is prepared request
a[0].url = a[0].url.replace(urllib.quote(","), ",")
return requests.Session.send(self, *a, **kw)
s = NoQuotedCommasSession()
s.get("http://somesite.com/an,url,with,commas,that,won't,be,encoded.")
回答by kujosHeist
The answers above didn't work for me.
上面的答案对我不起作用。
I was trying to do a get request where the parameter contained a pipe, but python requests would also percent encode the pipe. So instead i used urlopen:
我试图在参数包含管道的情况下执行 get 请求,但 python 请求也会对管道进行百分比编码。所以我改用了 urlopen:
# python3
from urllib.request import urlopen
base_url = 'http://www.example.com/search?'
query = 'date_range=2017-01-01|2017-03-01'
url = base_url + query
response = urlopen(url)
data = response.read()
# response data valid
print(response.url)
# output: 'http://www.example.com/search?date_range=2017-01-01|2017-03-01'
回答by Sandeep Kanabar
Please have a look at the 1st option in this github link. You can ignore the urlib
part which means prep.url = url
instead of prep.url = url + qry
请查看此github 链接中的第一个选项。你可以忽略这urlib
部分,prep.url = url
而不是prep.url = url + qry