如何防止python请求对我的URL进行百分比编码? [英] How to prevent python requests from percent encoding my URLs?

查看:124
本文介绍了如何防止python请求对我的URL进行百分比编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python中的request.get()获取以下格式的URL:

I'm trying to GET an URL of the following format using requests.get() in python:

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)

但是,URL得到了百分比编码,但我没有得到预期的响应.

However, the URL gets percent encoded and I don't get the expected response.

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 = 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?

谢谢!

推荐答案

这不是很好的解决方案,但是您可以使用string:

It is not good solution but you can use 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)

这篇关于如何防止python请求对我的URL进行百分比编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆