开发笔记

  • 首页
  • 工具箱
三味线的博客
  1. 首页
  2. JS/PY
  3. 正文

python requests 请求传参数空格处理

2020-06-06 960点热度 1人点赞 0条评论
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/zh_CN/latest/

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: Python
最后更新:2020-06-12

Clownce

不吃咸鱼的猫

点赞
< 上一篇

文章评论

取消回复

Captcha Code

COPYRIGHT © 2020 Clownce. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS

蜀ICP备18010095号