python库之requests
来源:互联网 发布:get it beauty2016 编辑:程序博客网 时间:2024/05/22 15:58
Requests库是python语言编写,基于urllib,完全满足http测试需求
1. 发送请求
使用Requests发送请求十分简单,get请求如下:
import requestsr = requests.get('http://www.baidu.com')
这样就实现了发送请求到百度,并且获得了一个Response对象,我们可以从这个对象中获得我们需要的所有信息
使用post请求方式同样简单,使用data
参数传递数据
r = requests.post('http://httpbin.org/post', data = {'key':'value'})
同样的,其他的请求方式也是这样:
>>> r = requests.put('http://httpbin.org/put', data = {'key':'value'})>>> r = requests.delete('http://httpbin.org/delete')>>> r = requests.head('http://httpbin.org/get')>>> r = requests.options('http://httpbin.org/get')
2. 向url传递参数
当想要向url中传递参数时,Requests允许使用元素为字符串类型的字典,使用params
关键字进行传递。例如:
payload = {'key1':'value1', 'key2':'value2'}r = requests.get('http://httpbin.org/get', params = payload)#输出urlprint r.url
还可以传递列表:
payload = {'key1': 'value1', 'key2': ['value2', 'value3']}r = requests.get('http://httpbin.org/get', params = payload)#输出urlprint r.url
3. 响应内容 r.text
可以通过r.text获取服务器的响应内容
import requestsr = requests.get("http://www.baidu.com")print r.text
r.text返回的是Unicode型数据,Requests库会自动将服务器的响应内容进行编码,通常的做法是,当发起请求时,Requests库会根据HTTP头信息,有针对性的猜测是什么类型的编码,然后将r.text进行编码。
可以使用r.encoding查看Requests猜测了哪种编码,并且可以进行修改,例如上面的例子
import requestsr = requests.get("http://www.baidu.com")#print r.textprint r.encoding
使用了ISO-8859-1编码,所以r.text中的中文显示乱码,接下来,我们更改编码格式:
import requestsr = requests.get("http://www.baidu.com")# print r.textr.encoding = 'utf-8'print r.encodingprint r.text
我们可以看到,编码变为了utf-8,同时中文显示正常
重点:
- 服务器返回的r.text是Unicode编码
- r.encoding改变的是r.text的编码,与r.content无关
4. 二进制响应内容
使用r.content,以二进制形式获取响应
r.text和r.content的区别:r.text返回的是Unicode型数据,r.content返回的是bytes型数据。也就是说,如果想获取文本,可以通过r.text,如果想获取图片、文件,则可以通过r.content
5. JSON响应内容
Requests有内置的JSON解码器,以防你处理json数据
import requestsimport jsonr = requests.post('https://api.github.com/some/endpoint', data=json.dumps({'some': 'data'}))print r.json()
如果响应中没有内容或者包含的是无效的JSON,当调用r.json()时会引发:ValueError: No JSON object could be decoded。例如:
import requestsimport jsonr = requests.get('http://www.baidu.com')print r.json()
6. 自定义Headers
自定义的Headers放于字典中,通过headers
参数进行传递
import requestsimport jsondata = {'some': 'data'}headers = {'content-type': 'application/json', 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}r = requests.get('http://www.baidu.com', data=data, headers=headers)r.encoding = 'utf-8'print(r.text)
7. 响应状态码status code
import requestsr = requests.get('http://www.baidu.com')print r.status_code
8. 响应头headers
使用r.headers获取响应头
import requestsr = requests.get('http://www.baidu.com')print r.headers
可以通过以下方式获取某个头信息
print r.headers['Content-Type']
print r.headers.get('Content-type')
9. Cookies
通过如下方式获取某个cookies值
>>> url = 'http://example.com/some/cookie/setting/url'>>> r = requests.get(url)>>> r.cookies['example_cookie_name']'example_cookie_value'
通过cookies
参数发送自己设置的cookies给服务器:
import requestscookies = {'cookies_are': 'working'}r = requests.get('http://httpbin.org/cookies', cookies = cookies)print r.text
- Python之requests库
- python库之requests
- python学习之 requests库
- Python学习之Requests库
- python简洁之道-----Requests库
- Python网络爬虫之Requests库
- Python库之Requests(iMooc)---(1)
- Python库之Requests(iMooc)---(2)
- Python库之Requests(iMooc)---(3)
- Python库之Requests(iMooc)---(4)
- Python库之Requests(iMooc)---(5)
- Python爬虫之requests
- python之requests篇
- Python之,requests包
- python爬虫之requests
- python requests库 笔记
- python requests库介绍
- python安装requests库
- 网站优化--YaHoo Web优化的14条法则
- 128. Longest Consecutive Sequence
- C++声明和定义
- react中的ajax封装
- Mybatis代码自动生成-mybatis-generator的使用
- python库之requests
- js中 arguments 对象
- CentOS 7 安装 Nginx 1.10.1.tar.gz
- 使用Markdown制作简历
- Neutron如何与Nova-Compute进行交互
- Logistic Regression分类器原理
- bootstrap响应式列重置、列偏移、列嵌套、列排序
- Linux命令——文件处理命令
- caffe学习笔记32-关于修改源码