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


0 0
原创粉丝点击