Python网络操作之requests模块
来源:互联网 发布:软件ui设计教程 编辑:程序博客网 时间:2024/06/05 17:42
出自:http://www.gaoxuewen.cn/index.php/python/1086.html
整理了一下requests模块的学习资料,Requests 使用的是 urllib3,因此继承了它的所有特性。Requests 支持 HTTP 连接保持和连接池,支持使用 cookie 保持会话,支持文件上传,支持自动确定响应内容的编码,支持国际化的 URL 和 POST 数据自动编码。
requests的安装:http://cn.python-requests.org/en/latest/user/install.html
实例方法:
”’
import requests
r = requests.get(‘http://github.com’)
r.url #查看r所对应的url
r.status_code #查看对应http返回状态码
r.encoding #查看返回内容字符集
r.text #查看返回的内容,会根据响应的头的字符集编码做动态调整
r.content #以字节的方式响应内容,如img类型的字节流
r.json() #内置的JSON解码器
r.headers #查看以一个Python字典形式展示的服务器响应头,根据 RFC 2616 , HTTP头部是大小写不敏感的。
r.request.headers #查看请求头
r.headers.get(‘content-type’) #获取content-type对应的值
r.cookies[‘example_cookie_name’]查看cookies信息,注意写法,是key value形式
r.history #查看请求历史
”’
请求超时设置
requests.get(‘http://github.com’, timeout=0.001) #仅对连接过程有效,与响应体的下载无关
响应的迭代器:
r = requests.get(‘http://www.baidu.com’, stream=True) #默认情况下,当你进行网络请求后,响应体会立即被下载。你可以通过 stream 参数覆盖这个行为,推迟下载响应体直到访问 Response.content 属性:
for line in r.iter_lines():
print line
Session会话保持:
s = requests.Session()
s.get(‘http://www.baidu.com’) #两次请求,系统会认为是一个user登录
s.get(‘http://www.baidu.com’)
POST请求
import requests
s = requests.Session()
s.get(“http://uliweb.cpython.org”)
token = s.cookies[“_csrf_token”]
d ={“csrf_token”:token,
“username”:”python”,
“password”:”python”}
r = s.post(“http://uliweb.cpython.org/login”,data=d)
print r.content
- Python网络操作之requests模块
- python的网络编程之requests模块
- Python网络请求模块requests
- python模块之requests学习
- python发送http请求之requests模块
- 每天一个python模块之Requests
- 每天一个python模块之Requests
- Python之Requests模块使用详解
- Python之Requests模块使用详解
- 每天一个python模块之Requests
- Python爬虫Requests模块系列之六
- python发送http请求之requests模块
- python发送http请求之requests模块
- python之网络I/O操作模块
- Python网络爬虫之Requests库
- python爬虫之urllib模块和requests模块学习
- Python模块学习 - Requests
- python requests 模块学习
- Java安全之认证与授权
- WEBLOGIC性能参数
- nyoj 素数环
- java面向对象
- Windows系统下php安装扩展mbstring
- Python网络操作之requests模块
- 第45条 通用程序设计——将局部变量的作用于最小化
- KMP算法
- Android混淆打包总结
- 【转】5分钟搞定android混淆
- Github新建仓库并关联代码
- Electron实战:创建ELectron开发的window应用安装包
- 主页面不出来的原因的排查
- 路径转为转义字符串