Python Requests库基本用法示例


Posted in Python onAugust 20, 2018

本文实例讲述了Python Requests库基本用法。分享给大家供大家参考,具体如下:

requests是python的一个http client库,提供了一套简捷的API供开发者使用。下面简单介绍一下其安装和使用。这里是官方文档。

0 安装

pip install requests

1 发送请求

r=requests.get('https://www.baidu.com')
print r.status_code,r.text
r=requests.post('http://httpbin.org/post')
r=requests.put('http://httpbin.org/put')
r=requests.delete('http://httpbin.org/delete')
r=requests.head('http://httpbin.org/head')
r=requests.options('http://httpbin.org/')

2 发送get参数

param={'key1':value1,'key2':value2}
r=requests.get('http://www.baidu.com/',params=param)

3 发送post参数

param={'key1':value1,'key2':value2}
r=requests.post('http://www.baidu.com/',params=param) #表单格式
r=requests.post('http://www.baidu.com/',json=param) #json格式数据
file= {'file':open('1.txt','rb')}
r=reuqest.post('http://httpbin.org/post',files=file)

4 文件下载

with open('1.pic','wb') as pic:
  for chunk in response.iter_content(size):
    pic.write(chunk)

5 携带header

header={'key1':value1,'key2':value2}
r=requests.get('http://www.baidu.com/',headers=header)

6 携带cookie

cookie={'key1':value1,'key2':value2}
r=requests.get('http://www.baidu.com/',cookies=cookie)

7 重定向

默认requests是允许重定向的,并将重定向的历史保存在response.history数组中
如果不需要重定向,可以通过开关来关闭

r=requests.get('http://www.baidu.com/',allow_redirects=False)

8 使用代理

使用socks代理需要安装三方扩展包

pip install requests[socks]
proxy={
  'http':'http://127.0.0.1:8000',
  'https':'https://127.0.0.1:8080'
  'http':'socks5://user:pass@127.0.0.1:8132'
}
r=requests.get('https://www.github.com/',proxies=proxy)

9 设置连接超时

r=requests.get('http://www.baidu.com/',timeout=2.5)

10 ssl证书

证书验证

requests.get('https://kennethreitz.com', verify=True)
requests.get('https://kennethreitz.com', cert=('/path/server.crt', '/path/key'))

如果指定本地证书及密钥,则密钥需要是解密的。

11 requests对象

r.url
r.text
r.headers

12 Response对象

response.request 对应的请求对象
response.raw socket上直接获得的数据
response.text 根据响应头进行解码的文本数据
response.content 不解码,返回二进制数据
response.json() 对返回数据进行json解码
response.headers 词典形式存储返回的headers
response.cookies 词典形式存储返回的cookies

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
python基础_文件操作实现全文或单行替换的方法
Sep 04 Python
python使用mysql的两种使用方式
Mar 07 Python
Python元组知识点总结
Feb 18 Python
Python中类的创建和实例化操作示例
Feb 27 Python
Python Lambda函数使用总结详解
Dec 11 Python
python基于opencv检测程序运行效率
Dec 28 Python
浅谈Python中的异常和JSON读写数据的实现
Feb 27 Python
python matplotlib:plt.scatter() 大小和颜色参数详解
Apr 14 Python
如何查看Django ORM执行的SQL语句的实现
Apr 20 Python
Python faker生成器生成虚拟数据代码实例
Jul 20 Python
python实现猜拳游戏项目
Nov 30 Python
PyTorch中permute的使用方法
Apr 26 Python
Django中使用第三方登录的示例代码
Aug 20 #Python
基于Django框架利用Ajax实现点赞功能实例代码
Aug 19 #Python
分析python请求数据
Aug 19 #Python
浅谈django orm 优化
Aug 18 #Python
django连接mysql配置方法总结(推荐)
Aug 18 #Python
python画一个玫瑰和一个爱心
Aug 18 #Python
python爱心表白 每天都是浪漫七夕!
Aug 18 #Python
You might like
PHP中Closure类的使用方法及详解
2015/10/09 PHP
PHP attributes()函数讲解
2019/02/03 PHP
XP折叠菜单&仿QQ2006菜单
2006/12/16 Javascript
js中单引号与双引号冲突问题解决方法
2013/10/04 Javascript
js 遍历json返回的map内容示例代码
2013/10/29 Javascript
JS实现两个大数(整数)相乘
2014/04/28 Javascript
原生js编写设为首页兼容ie、火狐和谷歌
2014/06/05 Javascript
浅谈Javascript数据属性与访问器属性
2016/07/26 Javascript
jQuery实现的自定义弹出层效果实例详解
2016/09/04 Javascript
javascript 动态脚本添加的简单方法
2016/10/11 Javascript
VUE利用vuex模拟实现新闻点赞功能实例
2017/06/28 Javascript
JS实现弹出下载对话框及常见文件类型的下载
2017/07/13 Javascript
JS实现简单的浮动碰撞效果示例
2017/12/28 Javascript
vue几个常用跨域处理方式介绍
2018/02/07 Javascript
javascript原生封装一个淡入淡出效果的函数测试实例代码
2018/03/19 Javascript
基于vue-upload-component封装一个图片上传组件的示例
2018/10/16 Javascript
记录vue做微信自定义分享的一些问题
2019/09/12 Javascript
js绘制一条直线并旋转45度
2020/08/21 Javascript
[02:07]2018DOTA2亚洲邀请赛主赛事第三日五佳镜头 fy极限反杀
2018/04/06 DOTA
Python中关于字符串对象的一些基础知识
2015/04/08 Python
python2.x实现人民币转大写人民币
2018/06/20 Python
详解Python3中的迭代器和生成器及其区别
2018/10/09 Python
对python创建及引用动态变量名的示例讲解
2018/11/10 Python
python实现简单的文字识别
2018/11/27 Python
一篇文章弄懂Python中所有数组数据类型
2019/06/23 Python
基于SQLAlchemy实现操作MySQL并执行原生sql语句
2020/06/10 Python
canvas实现图片马赛克的示例代码
2018/03/26 HTML / CSS
美国知名奢侈美容品牌零售商:Cos Bar
2017/04/21 全球购物
商务会议邀请函
2014/01/09 职场文书
幼儿园教师节活动方案
2014/02/02 职场文书
内科护士节演讲稿
2014/09/11 职场文书
2014年乡镇妇联工作总结
2014/12/02 职场文书
2015年办公室个人工作总结
2015/04/20 职场文书
新闻报道稿范文
2015/07/23 职场文书
祝福语集锦:给妹妹结婚的祝福语
2019/12/18 职场文书
uniapp开发打包多端应用完整方法指南
2022/12/24 Javascript