Python Requests库基本用法示例
2018-08-20 12:15
1056 查看
本文实例讲述了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采用requests库模拟登录和抓取数据的简单示例
- python中requests库session对象的妙用详解
- Python中第三方库Requests库的高级用法详解
- Python3.6安装及引入Requests库的实现方法
- python中数据爬虫requests库使用方法详解
- 基于python requests库中的代理实例讲解
- 使用requests库制作Python爬虫
- 解决Python requests库编码 socks5代理的问题
- python利用requests库进行接口测试的方法详解
- mac下给python3安装requests库和scrapy库的实例
- Python requests库用法实例详解
相关文章推荐
- for 循环语句基本用法及示例
- BeautifulSoup和lxml的基本用法示例
- SiteMesh基本用法及示例
- AspNetPager控件的最基本用法示例介绍
- log4j基本用法及简单配置示例
- Java操作FreeMarker模板引擎的基本用法示例小结
- SiteMesh基本用法及示例
- ASP.NET Dictionary 的基本用法示例介绍
- yii2缓存Caching基本用法示例
- Crosswalk基本用法示例(Demo)
- Reaver 基本用法示例
- JavaScript日期对象(Date)基本用法示例
- Freemarker基本用法示例
- Java的Spring框架的三种连接池的基本用法示例
- Android中ViewPager组件的基本用法及实现图片切换的示例
- Objective-C中NSArray的基本用法示例
- flex布局基本属性用法示例以及常见布局
- 【Android】11.6 Fragments基本用法示例
- linux不带缓冲的文件操作基本函数及用法示例
- Android 通知的基本用法示例代码