侧边栏壁纸
博主头像
三味的小站博主等级

世界上没有偶然,有的只是必然的结果。

  • 累计撰写 61 篇文章
  • 累计创建 13 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

python requests 请求传参数空格处理

三味线
2020-06-06 / 0 评论 / 0 点赞 / 8 阅读 / 1008 字
import json
import urllib
import requests
param1 = {'name': 'hello world'}
param2 = urllib.parse.urlencode({'name': 'hello world'}, quote_via=urllib.parse.quote)
r = requests.get("http://111.111.111.11:1111/api/_/getConfig", params=param1)
if (r.status_code != 200):
  exit(1)
print(r.url)
print(r.json())

直接传参数,即使用param1,空格被替换为 '+'

http://111.111.111.11:1111/api/_/getConfig?name=hello+world

使用urllib转换,即使用param2,空格被替换为 '%20'

http://111.111.111.11:1111/api/_/getConfig?name=hello%20world

另外,如果直接使用{'name': 'hello%20world'},会被转换成:

http://111.111.111.11:1111/api/_/getConfig?name=hello%2520world

手动替换时不能使用Map,需使用字符串

param = 'name=hello%20world'

Requests官方文档: https://requests.readthedocs.io/en/latest/

0

评论区