一、请求参数类型

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. RT-thread 设备驱动组件之PIN设备

    在RT-thread 2.0.0正式版中引入了pin设备作为杂类设备,其设备驱动文件pin.c在rt-thread-2.0.1\components\drivers\misc中,主要用于操作芯片GPI ...

  2. hdu 1690 Bus System (最短路径)

    Bus System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. powerdesigner 点击preview 语句出现 if exists

    drop table if exists XXX 这个是sql server 写法.如果要切换为 Oracle 写法 菜单栏: database-->change curren DBMS 选择你 ...

  4. Oracle 验证A表的2个字段组合不在B表2个字段组合里的数据

    select id, name from TAB_A t where not exists (select 1 from TAB_B t1 where t.id = t1.id and t.name ...

  5. [LouguT30212]玩游戏

    题面在这里 description 对于\(k=1,2,...,t\),求\[\frac{1}{nm}\sum_{i=1}^{n}\sum_{j=1}^{m}(a_i+b_j)^k\] 对\(9982 ...

  6. UVA.10066 The Twin Towers (DP LCS)

    UVA.10066 The Twin Towers (DP LCS) 题意分析 有2座塔,分别由不同长度的石块组成.现在要求移走一些石块,使得这2座塔的高度相同,求高度最大是多少. 问题的实质可以转化 ...

  7. AOJ 7.Redraiment猜想

    Redraiment猜想 Description redraiment在家极度无聊,于是找了张纸开始统计素数的个数. 设函数f(n)返回从1->n之间素数的个数. redraiment发现: f ...

  8. angularJS批量删除 品优购删除品牌(通用复选框批量选中删除解决思路)

    思路: 一步:在点击复选框时维护变量数组 在js中定义一个数组变量, 给复选框添加点击动作, 在动作中判断当前复选框是否为选中状态(即点击后复选框的是否选中状态), 若为选中状态,则向数组中添加选中的 ...

  9. noip模拟赛 大芳的逆行板载

    题目背景 大芳有一个不太好的习惯:在车里养青蛙.青蛙在一个n厘米(11n毫米s)的Van♂杆子上跳来跳去.她时常盯着青蛙看,以至于突然逆行不得不开始躲交叉弹.有一天他突发奇想,在杆子上每1厘米为一个单 ...

  10. SDRAM初始化

    DDR配置过程比较复杂,基本上是按照DDR控制器的时序要求来做的,其中很多参数要结合DDR芯片本身的参数来定,还有些参数是时序参数,要去详细计算.所以DDR配置非常繁琐.细致.专业.所以我们对DDR初 ...