python的request模块快速上手
1.引入模块:import request
2.可以使用get、post、put、delete、head、options,使用方法:r = request.get('www.baidu.com')
3.请求带参数:
payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
r = request.get(url,params=payload)
此时r.url得值为:url?key1=value1&key2=value2
4.post请求:
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post(url, data=payload)
5.定制请求头:
headers = {'user-agent': 'my-app/0.0.1'}
r = requests.get(url, headers = headers)
6.发送和获取cookie:
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies) //发送
r.cookies['example_cookie_name'] //获取
7.常用属性和方法:
r.text : 获取服务器相应得内容
r.encoding = 'utf-8' : 设置编码方式
r.json() : json解码
r.status_code : 检测响应状态码
初级参考原文:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html
高级用法请看:http://docs.python-requests.org/zh_CN/latest/user/advanced.html#advanced