Requests库
Requests 是使用Apache2 Licensed 许可证的HTTP 库。用Python 编写。
Requests 使用的是urllib3,因此继承了它的所有特性。Requests 支持HTTP 连接保持和连接池,支持使用cookie 保持会话,支持文件上传,支持自动确定响应内容的编码,支持国际化的URL 和POST 数据自动编码。
1.Requests安装
下载地址:https://pypi.python.org/pypi/requests/
官方网站:http://docs.python-requests.org/en/master/
中文文档:http://cn.python-requests.org/zh_CN/latest/
2.Demo
>>> r = requests.get('https://api.github.com/user', auth=('user', 'psw')) >>> r.status_code 200 >>> r.headers['content-type'] 'application/json; charset=utf8' >>> r.encoding 'utf-8' >>> r.text u'{"type":"User"...' >>> r.json() {u'private_gists': 419, u'total_private_repos': 77, ...}
其中auth=('user', 'psw')为github系统注册的账号和密码
3.Post请求模拟登录
import requests url1 = 'http://www.exanple.com/login'#登录地址 url2 = "http://www.example.com/main"#需要登录才能访问的地址 data={"user":"user","password":"pass"} headers = { "Accept":"text/html,application/xhtml+xml,application/xml;", "Accept-Encoding":"gzip", "Accept-Language":"zh-CN,zh;q=0.8", "Referer":"http://www.example.com/", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36" } res1 = requests.post(url1, data=data, headers=headers) res2 = requests.get(url2, cookies=res1.cookies, headers=headers) print(res2.content)#获得二进制响应内容 print(res2.raw)#获得原始响应内容,需要stream=True print(res2.raw.read(50)) print(type(res2.text))#返回解码成unicode的内容 print(res2.url) print(res2.history)#追踪重定向 print(res2.cookies) print(res2.cookies['example_cookie_name']) print(res2.headers) print(res2.headers['Content-Type']) print(res2.headers.get('content-type')) print(res2.json)#讲返回内容编码为json print(res2.encoding)#返回内容编码 print(res2.status_code)#返回http状态码 print(res2.raise_for_status())#返回错误状态码
4.使用Session()对象的写法
import requests s = requests.Session() url1 = 'http://www.exanple.com/login'#登录地址 url2 = "http://www.example.com/main"#需要登录才能访问的页面地址 data={"user":"user","password":"pass"} headers = { "Accept":"text/html,application/xhtml+xml,application/xml;", "Accept-Encoding":"gzip", "Accept-Language":"zh-CN,zh;q=0.8", "Referer":"http://www.example.com/", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36" } res1 = s.post(url1, data=data) res2 = s.post(url2) print(res2.content)