一、请求参数类型

1.get

requests.get(url, params, cookies=cookies)

url:字符串;

params:字典类型,可以为空;

cookies:字典类型,可以为空;

无headers参数;

2.post

requests.post(url, data, headers=headers, cookies=cookies)

post请求根据编码方式有可分为:application/x-www-form-urlencoded、multipart/form-data、application/json、text/xml。

application/x-www-form-urlencoded

浏览器的原生 form 表单,如果不设置 enctype 属性,那么最终就会以 application/x-www-form-urlencoded 方式提交数据。

data:字典类型;

headers:字典类型,可以为空;

cookies:字典类型,可以为空;

url = 'http://httpbin.org/post'
d = {'key1': 'value1', 'key2': 'value2'}
r = requests.post(url, data=d)
print r.text

application/json

data:json类型;

headers:字典类型,可以为空;

cookies:字典类型,可以为空;

url = 'http://httpbin.org/post'
s = json.dumps({'key1': 'value1', 'key2': 'value2'})
r = requests.post(url, data=s)
print r.text

其他参考https://www.cnblogs.com/insane-Mr-Li/p/9145152.html

3.put

requests.put(url, data, headers=headers, cookies=cookies)

与post基本一致

4.delete

requests.delete(url, headers=headers, cookies=cookies)

headers:字典类型,可以为空;

cookies:字典类型,可以为空;

无data参数;

二、数据类型转换

a='{"errno":0,"errmsg":"这是一个实例","unassigned":0,"total":0,"list":null}'

字符串转换成字典

字符串中无值为null时,可以使用eval();

字符串中是单引号时,可以使用eval();

字符串中有值为null时,可以使用json.loads();

字符串中是双引号时,可以使用json.loads();

参考https://www.cnblogs.com/lansan0701/p/9917094.html

字典转化成字符串

json.dumps();

字典中有中文时,需加 ensure_ascii=False 参数;

>>> a='{"errno":0,"errmsg":"这是一个实例","unassigned":0,"total":0,"list":null}'
>>> eval(a)
NameError: name 'null' is not defined
>>> import json
>>> json.loads(a)
{'total': 0, 'errno': 0, 'errmsg': '这是一个实例', 'unassigned': 0, 'list': None}
>>> json.dumps(a)
'"{\\"errno\\":0,\\"errmsg\\":\\"\\u8fd9\\u662f\\u4e00\\u4e2a\\u5b9e\\u4f8b\\",\\"unassigned\\":0,\\"total\\":0,\\"list\\":null}"'
>>> json.dumps(a, ensure_ascii=False)
'"{\\"errno\\":0,\\"errmsg\\":\\"这是一个实例\\",\\"unassigned\\":0,\\"total\\":0,\\"list\\":null}"'

三、获取cookies

res = requests.post('http://www.xxx.com', {'username':'lilei', 'password':'123456'})

cookies = res.cookies #此处类型为<class 'requests.cookies.RequestsCookieJar'>

cookies = requests.utils.dict_from_cookiejar(cookies) #将CookieJar转为字典

python之requests库使用问题汇总的更多相关文章

  1. 【转】使用Python的Requests库进行web接口测试

    原文地址:使用Python的Requests库进行web接口测试 1.Requests简介 Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写, ...

  2. Python爬虫—requests库get和post方法使用

    目录 Python爬虫-requests库get和post方法使用 1. 安装requests库 2.requests.get()方法使用 3.requests.post()方法使用-构造formda ...

  3. python中requests库使用方法详解

    目录 python中requests库使用方法详解 官方文档 什么是Requests 安装Requests库 基本的GET请求 带参数的GET请求 解析json 添加headers 基本POST请求 ...

  4. 解决python的requests库在使用过代理后出现拒绝连接的问题

    在使用过代理后,调用python的requests库出现拒绝连接的异常 问题 在windows10环境下,在使用代理(VPN)后.如果在python中调用requests库来地址访问时,有时会出现这样 ...

  5. python利用requests库模拟post请求时json的使用

    我们都见识过requests库在静态网页的爬取上展现的威力,我们日常见得最多的为get和post请求,他们最大的区别在于安全性上: 1.GET是通过URL方式请求,可以直接看到,明文传输. 2.POS ...

  6. python导入requests库一直报错原因总结 (文件名与库名冲突)

    花了好长时间一直在搞这个 源代码: 一直报如下错误: 分析原因: 总以为没有导入requests库,一直在网上搜索各种的导入库方法(下载第三方的requests库,用各种命令工具安装),还是报错 后来 ...

  7. python爬虫---requests库的用法

    requests是python实现的简单易用的HTTP库,使用起来比urllib简洁很多 因为是第三方库,所以使用前需要cmd安装 pip install requests 安装完成后import一下 ...

  8. Python爬虫---requests库快速上手

    一.requests库简介 requests是Python的一个HTTP相关的库 requests安装: pip install requests 二.GET请求 import requests # ...

  9. python 之Requests库学习笔记

    1.    Requests库安装 Windows平台安装说明: 直接以管理员身份打开cmd运行界面,使用pip管理工具进行requests库的安装. 具体安装命令如下: >pip instal ...

随机推荐

  1. bzoj 2424: [HAOI2010]订货 (费用流)

    直接费用流,天数就是点数 type arr=record toward,next,cap,cost:longint; end; const maxm=; maxn=; mm=<<; var ...

  2. NVIDIA TensorRT 让您的人工智能更快!

    NVIDIA TensorRT 让您的人工智能更快! 英伟达TensorRT™是一种高性能深度学习推理优化器和运行时提供低延迟和高通量的深度学习推理的应用程序.使用TensorRT,您可以优化神经网络 ...

  3. BZOJ1027 [HNOI2004]打鼹鼠 【dp】

    1207: [HNOI2004]打鼹鼠 Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 3647  Solved: 1746 [Submit][Sta ...

  4. HDOJ.1051 Wooden Sticks (贪心)

    Wooden Sticks 点我挑战题目 题意分析 给出T组数据,每组数据有n对数,分别代表每个木棍的长度l和重量w.第一个木棍加工需要1min的准备准备时间,对于刚刚经加工过的木棍,如果接下来的木棍 ...

  5. HDU 2094 拓扑排序

    产生冠军 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  6. 一个完整的upstart脚本分析

    基本概念可以了解 http://www.mike.org.cn/articles/understand-upstart/ http://blog.fens.me/linux-upstart/ http ...

  7. ubuntu切割mp3文件

    ffmpeg -i InputFile -vn -acodec copy -ss 00:00:00 -t 00:01:32 OutPutFile

  8. ZooKeeper管理员指南——部署与管理ZooKeeper

    1.部署 本章节主要讲述如何部署ZooKeeper,包括以下三部分的内容: 系统环境 集群模式的配置 单机模式的配置 系统环境和集群模式配置这两节内容大体讲述了如何部署一个能够用于生产环境的ZK集群. ...

  9. Moodle通过CLI安装

    Moodle通过CLI安装 前提:Moodle准备工作已经完成 1) 数据库(及用户) 2) moodledata目录 3) 源代码及站点配置 安装过程 打开终端,或通过Putty或Xshell等软件 ...

  10. 基于http的追加协议、构建web内容的技术、web的攻击技术(9,10,11)

    第九章 基于http的追加协议 用来提升http的瓶颈,比如Ajax技术,spdy等 第十章 构建web内容的技术 html.css.js等 第十一章 web的攻击技术 比如sql注入攻击.xss等.