1 2 3 4 5
| 虽然Python的标准库中urllib2模块已经包含了平常我们使用的大多数功能
但是它的API使用起来让人感觉不太好
而Requests自称"HTTP for Humans",说明使用更简洁方便
|
Requests继承了urllib2的所有特性
1 2 3 4 5
| Requests支持HTTP连接保持和连接池 Requests支持使用cookie保持会话 Requests支持文件上传 Requests支持自动确定响应内容的编码 Requests支持国际化的URL和POST数据自动编码
|
1 2 3 4 5 6 7
| requests的底层实现其实就是urllib3
Requests的文档非常完备,中文文档也相当不错。 Requests能完全满足当前网络的需求,支持Python 2.6—3.5,而且能在PyPy下完美运行。
开源地址:https://github.com/kennethreitz/requests 中文文档API:http://docs.python-requests.org/zh_CN/latest/index.html
|
安装方式
1 2 3 4 5
| 利用pip安装或者利用 easy_install 都可以完成安装:
$ pip install requests
$ easy_install requests
|
基本GET请求(headers参数和parmas参数)
1 2 3 4 5
| import requests
response = requests.get("http://www.baidu.com/") print response.status_code
|
添加headers和查询参数
1 2 3 4
| 如果想添加headers, 可以传入headers参数来增加请求头中的headers信息。
如果要将参数放在url中传递,可以利用params参数。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import requests
kv = {'wd': 'elasticsearch'}
headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
response = requests.get("http://www.baidu.com/s?", params=kv, headers=headers)
print response.url
print response.encoding
print response.status_code
|
1 2 3 4
| 使用response.text时,Requests会基于HTTP响应的文本编码自动解码响应内容, 大多数Unicode字符集都能被无缝地解码。
使用response.content时,返回的是服务器响应数据的原始二进制字节流,可以用来保存图片等二进制文件。
|
基本POST请求(data参数)
1 2
| 1. 最基本的GET请求可以直接用post方法 response = requests.post("http://www.baidu.com/", data = data)
|
1 2
| 2. 传入data数据 对于 POST 请求来说,我们一般需要为它增加一些参数。那么最基本的传参方法可以利用data这个参数。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import requests
formdata = { "type":"AUTO", "i":"i love python", "doctype":"json", "xmlVersion":"1.8", "keyfrom":"fanyi.web", "ue":"UTF-8", "action":"FY_BY_ENTER", "typoResult":"true" }
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null"
headers={ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"}
response = requests.post(url, data = formdata, headers = headers)
print response.json()
|

代理(proxies参数)
1 2 3 4 5 6 7 8 9 10 11
| import requests
proxies = { "http": "http://12.34.56.79:9527", "https": "http://12.34.56.79:9527", }
response = requests.get("http://www.baidu.com", proxies = proxies) print response.text
|
私密代理验证(特定格式) 和 Web客户端验证(auth 参数)
1
| urllib2这里的做法比较复杂,requests只需要一步:
|
1 2 3 4 5 6 7 8
| import requests
proxy = { "http": "mr_mao_hacker:sffqry9r@61.158.163.130:16816" }
response = requests.get("http://www.baidu.com", proxies = proxy)
print response.text
|
web客户端验证
1
| 如果是Web客户端验证,需要添加 auth = (账户名, 密码)
|
1 2 3 4 5 6 7
| import requests
auth=('test', '123456')
response = requests.get('http://192.168.199.107', auth = auth)
print response.text
|
Cookies和Session
1
| 如果一个响应中包含了cookie,那么我们可以利用cookies参数拿到:
|
1 2 3 4 5 6 7 8 9 10 11 12
| import requests
response = requests.get("http://www.baidu.com/")
cookiejar = response.cookies
cookiedict = requests.utils.dict_from_cookiejar(cookiejar)
print cookiejar print cookiedict
|
Session
1 2 3 4
| 在requests里,session对象是一个非常常用的对象, 这个对象代表一次用户会话:从客户端浏览器连接服务器开始,到客户端浏览器与服务器断开。
会话能让我们在跨请求时候保持某些参数,比如在同一个Session实例发出的所有请求之间保持cookie 。
|
实现人人网登录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import requests
ssion = requests.session()
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
data = {"email":"mr_mao_hacker@163.com", "password":"alarmchime"}
ssion.post("http://www.renren.com/PLogin.do", data = data)
response = ssion.get("http://www.renren.com/410043129/profile")
print response.text
|

处理HTTPS请求/SSL证书验证
1 2 3
| Requests也可以为HTTPS请求验证SSL证书:
要想检查某个主机的SSL证书,你可以使用verify参数(也可以不写)
|
1 2 3 4 5 6
| import requests response = requests.get("https://www.baidu.com/", verify=True)
print r.text
|
1
| 如果SSL证书验证不通过,或者不信任服务器的安全证书,则会报出SSLError,据说12306证书是自己做的
|
1 2 3
| import requests response = requests.get("https://www.12306.cn/mormhweb/") print response.text
|

1
| 如果我们想跳过 12306 的证书验证,把verify设置为False就可以正常请求了。
|
1 2 3 4 5 6 7 8
| import requests import sys reload(sys) sys.setdefaultencoding("utf-8")
r = requests.get("https://www.12306.cn/mormhweb/", verify = False) print r.text
|