10.Python-第三方库requests详解(二)

Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库。它比 urllib 更加方便,能够节约咱们大量的工做,彻底知足 HTTP 测试需求。Requests 的哲学是以 PEP 20 的习语为中心开发的,因此它比 urllib 更加 Pythoner。更重要的一点是它支持 Python3 哦!php

  • Beautiful is better than ugly.(美丽优于丑陋)
  • Explicit is better than implicit.(清楚优于含糊)
  • Simple is better than complex.(简单优于复杂)
  • Complex is better than complicated.(复杂优于繁琐)
  • Readability counts.(重要的是可读性)

1、安装 Requests

1.经过pip安装 pip install Requestshtml

2.使用IDE安装好比 pycharmpython

2、发送请求get与传递post参数

先来一个简单的例子吧!让你了解下其威力:json

1 # r = requests.get(url='http://www.itwhy.org')  #最基本的get请求
2 # print r.status_code  #返回状态
3 # r= requests.get(url='http://www.baidu.com/',params={'wd':'python'})  #带参数的get请求
4 # print r.url
5 # print r.text  #d打印解码后的返回数据

HTTP不只get这么简单,其余的方式也是一句话哦cookie

1 get = requests.get('http://httpbin.org') #get请求
2 post = requests.post('https://httpbin.org/post')  #post请求
3 put = requests.put('http://httpbin.org/put') #put请求
4 delete = requests.delete('http://httpbin.org/delete') #delete 请求
5 head = requests.head('http://httpbin.org/get') #head请求
6 options = requests.options('http://httpbin.org/get')  #option 请求

已上的HTTP方法,对于WEB系统通常只支持GET 和post 有一些还支持HEAD方法。数据结构

1 #带参数的请求实例
2 
3 requests.get('http://www.dict.baidu.com/s',params={'wd':'python'})
4 requests.post('http://www.itwhy.org/wp-comments-post.php',data = {'comment':'测试post'})

post传送数据的几种方法post

若是咱们要发送复杂的表单,就须要POST数据了。和GET传送数据同样,想方法中额外添加一个data参数的事儿。这种方式至关于你在表单中填写这些数据,而后点击表单的提交。测试

1.提交表单数据url

1 data = {
2     'name':'yitian',
3     'age':22,
4     'friends':['zhang3','li4']   #建立一个data表单数据
5 }
6 response = requests.post('{base_url}post',data=data) #传入一个字典data

 

2.提交字符串数据spa

有时候POST数据不是使用表单方式,而是直接在请求体中附加参数。那么咱们在发送参数的时候不能向data参数添加字典了,而应该传递字符串。

1 response = requests.post('{base_url}post',data=json.dumps(data)) #上面的例子就是将data字典数据转成json数据结构

json.loads() : 将json结构转变成Python数据结构

json.dumps():将Python数据结构转变成json结构

json.load(): 读取json文件转成Python数据结构

json.dump():写入json文件,读取json文件而后转成Python数据结构

 

3.直接提交JSON字符串看成总体传送

有些程序(例如Github的API)须要将JSON字符串直接当作请求体发送,好比说上面这种将字典转换为JSON的例子。在这种状况下,咱们能够直接将字典的引用传递给方法的json参数,这样就不须要咱们手动转换,requests会自动转换。

1 response = requests.post('{base_url}post',json=data) 

 

4.上传文件

在网页上,上传头像等操做都须要上传multipart/form-data类型的表单。使用requests也很是简单。须要注意打开文件的时候最好使用二进制模式,使用文本模式打开文件可能致使requests不能正确计算文件的大小。

file = open(r'c:\Windows\System32\drivers\etc\hosts', mode='rb')  #rb是以二进制读取方式

data = {
    'file': file
}

response = requests.post(f'{base_url}post', files=data)
print(response.text)

requests 支持流式上传的,容许发送大的数据流或文件而无需先把他读入内存。需使用流式上传
1 with open('massive-body') as f:
2     requests.post('http://some.url/streamed',data=f)

 

cookie 操做

若是要获取响应的cookies,调用cookies属性便可,它会返回一个对象,它实现了标准库的http.cookiejar。因此咱们能够按照cookiejar的
方法来使用。好比说访问百度的时候,它会分配一个cookie,因此咱们可使用下面的代码获取Cookie。
RequestsCookieJarRequestsCookieJar
1 cookies = requests.cookies.RequestsCookieJar()
2 cookies.set('name','yitian')
3 response = requests.get('{base_urlcookies',cookies=cookies)
4 print response.text
相关文章
相关标签/搜索