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类装饰器用法实例
Jun 04 Python
Python while 循环使用的简单实例
Jun 08 Python
各种Python库安装包下载地址与安装过程详细介绍(Windows版)
Nov 02 Python
python使用正则表达式匹配字符串开头并打印示例
Jan 11 Python
python 读取文本文件的行数据,文件.splitlines()的方法
Jul 12 Python
Python实现Mysql数据统计及numpy统计函数
Jul 15 Python
Flask框架学习笔记之路由和反向路由详解【图文与实例】
Aug 12 Python
通过实例解析python描述符原理作用
Jan 22 Python
借助Paramiko通过Python实现linux远程登陆及sftp的操作
Mar 16 Python
django 模版关闭转义方式
May 14 Python
详解pyinstaller生成exe的闪退问题解决方案
Jun 19 Python
教你怎么用Python监控愉客行车程
Apr 29 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查找指定目录下指定大小文件的方法
2014/11/28 PHP
利用laravel+ajax实现文件上传功能方法示例
2017/08/13 PHP
PHP经典设计模式之依赖注入定义与用法详解
2019/05/21 PHP
JS验证控制输入中英文字节长度(input、textarea等)具体实例
2013/06/21 Javascript
jquery ajax中使用jsonp的限制解决方法
2013/11/22 Javascript
jquery如何实现锚点链接之间的平滑滚动
2013/12/02 Javascript
模拟用户点击弹出新页面不会被浏览器拦截
2014/04/08 Javascript
JS获取表格内指定单元格html内容的方法
2015/03/31 Javascript
javascript删除元素节点removeChild()用法实例
2015/05/26 Javascript
jQuery短信验证倒计时功能实现方法详解
2016/05/25 Javascript
jQuery的图片轮播插件PgwSlideshow使用详解
2016/08/11 Javascript
原生js图片轮播效果实现代码
2016/10/19 Javascript
JS碰撞运动实现方法详解
2016/12/15 Javascript
详解react如何在组件中获取路由参数
2017/06/15 Javascript
基于vue hash模式微信分享#号的解决
2020/09/07 Javascript
离线安装Pyecharts的步骤以及依赖包流程
2020/04/23 Python
Python3 适合初学者学习的银行账户登录系统实例
2017/08/08 Python
浅谈django开发者模式中的autoreload是如何实现的
2017/08/18 Python
python使用TensorFlow进行图像处理的方法
2018/02/28 Python
Python实现的knn算法示例
2018/06/14 Python
mvc框架打造笔记之wsgi协议的优缺点以及接口实现
2018/08/01 Python
在Python运行时动态查看进程内部信息的方法
2019/02/22 Python
彼得罗夫美国官网:Peter Thomas Roth美国(青瓜面膜)
2017/11/05 全球购物
世界领先的豪华床上用品供应商之一:Bedeck Home
2019/03/18 全球购物
英国健身超市:Fitness Superstore
2019/06/17 全球购物
物业管理应届生求职信
2013/10/28 职场文书
大学生职业规划前言模板
2013/12/27 职场文书
关于旷工的检讨书
2014/02/02 职场文书
上班看电影检讨书
2014/02/12 职场文书
IT工程师岗位职责
2014/07/04 职场文书
2015年爱牙日活动总结
2015/02/05 职场文书
会计工作能力自我评价
2015/03/05 职场文书
《雪地里的小画家》教学反思
2016/02/16 职场文书
导游词之澳门妈祖庙
2019/12/19 职场文书
mysql 8.0.24版本安装配置方法图文教程
2021/05/12 MySQL
浅谈Node的内存泄露问题
2022/05/06 NodeJs