一、请求参数类型

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. Java入门之:基本数据类型

    Java基本数据类型 变量就是申请内存来存储值,也就是说,当创建变量的时候,需要在内存中申请空间.内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来存储该类型的数据,如下图所示: 因此, ...

  2. poj2018——Best Cow Fences

    Description Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each fie ...

  3. css边框以及其他常用样式

    1. 边框是1像素,实体的,红色的. <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  4. [BZOJ5292] [BJOI2018]治疗之雨

    题目链接 BZOJ:https://lydsy.com/JudgeOnline/problem.php?id=5292 洛谷:https://www.luogu.org/problemnew/show ...

  5. [HNOI2010]合唱队 区间DP

    ---题面--- 题解: 偶然翻到这道题,,,就写了. 观察到一个数被插在哪里只受前一个数的影响,如果明确了前一个数是哪个,那么我们就可以确定大小关系,就可以知道当前这个数插在哪里,而上一个插入的数就 ...

  6. [LNOI] 相逢是问候 || 扩展欧拉函数+线段树

    原题为2017六省联考的D1T3 给出一个序列,m次操作,模数p和参数c 操作分为两种: 1.将[l,r]区间内的每个数x变为\(c^x\) 2.求[l,r]区间内数的和%p 首先,我们要了解一些数论 ...

  7. BZOJ3172 & 洛谷3966 [Tjoi2013]单词 【fail树】

    3172: [Tjoi2013]单词 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 4293  Solved: 2083 [Submit][Stat ...

  8. HDOJ(HDU).2266 How Many Equations Can You Find (DFS)

    HDOJ(HDU).2266 How Many Equations Can You Find (DFS) [从零开始DFS(9)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零 ...

  9. mac, xcode 6.1 安装command line tools 支持,autoconf,automake等

    以下软件包 都去我的环境库找到 1 先安装 tcl库 2 安装macports /opt/local/bin/port 一般装到这里 安装autoconf时提示: Warning: The Xcode ...

  10. C++分离字符串中的数字和字符 转

    #include <iostream> #include <string> #include <vector> using namespace std; void ...