python之requests库使用问题汇总
一、请求参数类型
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库使用问题汇总的更多相关文章
- 【转】使用Python的Requests库进行web接口测试
原文地址:使用Python的Requests库进行web接口测试 1.Requests简介 Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写, ...
- Python爬虫—requests库get和post方法使用
目录 Python爬虫-requests库get和post方法使用 1. 安装requests库 2.requests.get()方法使用 3.requests.post()方法使用-构造formda ...
- python中requests库使用方法详解
目录 python中requests库使用方法详解 官方文档 什么是Requests 安装Requests库 基本的GET请求 带参数的GET请求 解析json 添加headers 基本POST请求 ...
- 解决python的requests库在使用过代理后出现拒绝连接的问题
在使用过代理后,调用python的requests库出现拒绝连接的异常 问题 在windows10环境下,在使用代理(VPN)后.如果在python中调用requests库来地址访问时,有时会出现这样 ...
- python利用requests库模拟post请求时json的使用
我们都见识过requests库在静态网页的爬取上展现的威力,我们日常见得最多的为get和post请求,他们最大的区别在于安全性上: 1.GET是通过URL方式请求,可以直接看到,明文传输. 2.POS ...
- python导入requests库一直报错原因总结 (文件名与库名冲突)
花了好长时间一直在搞这个 源代码: 一直报如下错误: 分析原因: 总以为没有导入requests库,一直在网上搜索各种的导入库方法(下载第三方的requests库,用各种命令工具安装),还是报错 后来 ...
- python爬虫---requests库的用法
requests是python实现的简单易用的HTTP库,使用起来比urllib简洁很多 因为是第三方库,所以使用前需要cmd安装 pip install requests 安装完成后import一下 ...
- Python爬虫---requests库快速上手
一.requests库简介 requests是Python的一个HTTP相关的库 requests安装: pip install requests 二.GET请求 import requests # ...
- python 之Requests库学习笔记
1. Requests库安装 Windows平台安装说明: 直接以管理员身份打开cmd运行界面,使用pip管理工具进行requests库的安装. 具体安装命令如下: >pip instal ...
随机推荐
- 【bzoj4921】[Lydsy六月月赛]互质序列 暴力
题目描述 给出一个序列,要求删除一段非空区间,使得剩下的数的个数大于等于2.求所有删除方式剩下的数的最大公约数的和. 输入 第一行包含一个正整数n(3<=n<=100000),表示序列的长 ...
- Codeforces Round #510 Div. 2 Virtual Participate记
这场打的顺手到不敢相信.如果不是vp的话估计肯定打不到这个成绩. A:最大显然,最小的话每次暴力给最小的+1. #include<iostream> #include<cstdio& ...
- BZOJ4815 [CQOI2017]小Q的表格 【数论 + 分块】
题目链接 BZOJ4815 题解 根据题中的式子,手玩一下发现和\(gcd\)很像 化一下式子: \[ \begin{aligned} bf(a,a + b) &= (a + b)f(a,b) ...
- AOJ.720 丢失的学妹
缺失的学妹 考察点 STL MAP Time Mem Len Lang 3.81s 39.1MB 0.68K G++ 题意分析 给出妹子学号的个数n,给出n个学号,和n-1个学号,求在n学号中那个没有 ...
- Kindle 电子书相关的工具软件【转】
这里是与 Kindle 电子书相关的工具软件.它们可以帮助我们解决在日常使用电子书时所可能遇到的问题,比如 kindle 管理工具.kindle 转换工具.kindle电子书制作工具.kindle 推 ...
- Machine Learning CodeForces - 940F (带修改的莫队)
You come home and fell some unpleasant smell. Where is it coming from? You are given an array a. You ...
- [转载]系统管理:update-alternatives
http://blog.csdn.net/dbigbear/article/details/4398961 好吧,其实博主也是转载的. update-alternatives --display | ...
- POJ 1698 最大流
Alice's Chance Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7327 Accepted: 2992 De ...
- emoji表情处理研究
http://blog.csdn.net/qdkfriend/article/details/7576524
- UVA 1210 Sum of Consecutive Prime Numbers
https://vjudge.net/problem/UVA-1210 统计质数前缀和,枚举左右端点,这一段的区间和+1 #include<cstdio> #define N 10001 ...