import requests
下面就可以使用神奇的requests模块了!
1.向网页发送数据
>>> payload = {'key1': 'value1', 'key2': ['value2', 'value3']} >>> r = requests.get('http://httpbin.org/get', params=payload)
>>> print(r.url)
http://httpbin.org/get?key1=value1&key2=value2&key2=value3
post与get方法都类似,只不过不是params=...而是data=...
>>> r = requests.put("http://httpbin.org/put")
>>> r = requests.delete("http://httpbin.org/delete")
>>> r = requests.head("http://httpbin.org/get")
>>> r = requests.options("http://httpbin.org/get")
2.把Response保存到文件里(官方文档推荐的方法)
with open(filename, 'wb') as fd:
for chunk in r.iter_content(chunk_size):
fd.write(chunk)
3. r的一些属性
r.encoding #编码
r.status_code #状态码(404,200等)
r.raise_for_status() #如果状态码不是200会发起报错
4.高级用法(会话对象——仿真网页操作):http://cn.python-requests.org/zh_CN/latest/user/advanced.html#advanced