接口自动化测试复习day06-requests

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Richard_Kong
'''
解决乱码问题,就是编码格式问题response.encoding = 'utf-8'
'''
import requests
response = requests.get("http://www.baidu.com")
print(response.encoding)# 默认的编码格式为ISO-8859-1,需要设置编码格式
response.encoding = 'utf-8'
print(response.text)
print(response.encoding)
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Richard_Kong
'''
如何传递url参数
1、先导包
2、发送请求
3、查看响应
'''
import requests
# 可以直接将参数写在url中 url和参数之间用英文的?隔开
url = 'http://localhost/Home/Goods/search.html?q=iPhone'

# 可以通过params传递参数
parameters = "q = iPhone"
response = requests.get(url=url,params=parameters)
print(response.text)
# 也可以通过字典来传递参数
dic = {
    "q":"iPhone"
}
response = requests.get(url=url,params=dic)
print(response.text)
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Richard_Kong
"""
发送post请求
response = requests.post(url,data,json)
url : 请求的url
data: 可选 要发送到请求体中的字典、元组、字节或文件对象
json:要发送到请求体中的json数据
说明:
    data:参数接收form表单数据,后台会自动附加form表单请求信息头(data数据格式为字典)
    json:参数接收json数据,后台会自动附加json表单请求头信息
        headers  = {"Content-Type":"application/json"}
需求:
    TPSHOP 项目的登陆接口:请求数据 username:15958186974 password:123456 verify_code = 8888
"""
import requests

login_url = "http://ihrm-test.itheima.net/api/sys/login"
login_data = {
    "mobile": "13800000002",
    "password": "123456",
}
response = requests.post(url=login_url, json=login_data)
print(response.json())
tp_url = "http://localhost/index.php?m=Home&c=User&a=do_login"
tp_data = {
    "username":"15958186974",
    "password":"123456",
    "verify_code":"8888"
    # 不管输送啥验证码都是错误 这个是cookie的问题,后续解决
}
response = requests.post(url=tp_url,data=tp_data)
print(response.json())

4、其他的请求方式作为了解

  put:修改资源

  delete:删除资源

  head:响应数据中只包含请求头,前提是项目中已实现该功能

  options:查看接口支持的请求方法,前提是项目已经实现该功能

5、响应内容解析

  响应的用途就是断言,查看输出和预期输出是否一致

  实现:

    response.status_code 响应状态码

    response.url:url地址信息

    response.encoding:查看响应数据编码格式

    response.headers:查看头部信息

    response.cookies:查看cookies信息

    response.text:文本形式查看响应数据

    response.content: 字节码形式查看响应数据

    response.json():Json形式查看响应数据

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Richard_Kong
import requests
response = requests.get("http://www.baidu.com")
response.encoding = "utf-8"
print(response.encoding) # 查看响应数据编码格式
print(response.status_code) # 查看响应状态码
print(response.url) # 查看url地址信息
print(response.headers) # 查看请求的头部信息
print(response.headers.get("Content-Type")) # 获取头部信息的 Content-Type值 使用header.get("Content-Type")的方式
print(response.cookies) # 获取cookie对象,可以返回多个cookie对象
print(response.cookies.get("BDORZ")) # 获取cookie对象的值
print(response.cookies.keys()) # 获取cookie的keys
print(response.cookies.values())# 获取cookie的value
print(response.text) # 文本形式查看响应数据
print(response.content)# 以字节码的形式查看响应数据
print(response.content.decode("utf-8")) # 字节码形式查看 之后 解码为utf-8进行查看

 获取json格式的返回数据,当接口的返回数据不是json格式数据时,使用response.json()获取返回数据会报错

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Richard_Kong
"""
1). 访问查询天气信息的接口,并获取JSON响应数据
2). 接口地址:http://www.weather.com.cn/data/sk/101010100.html
"""
import requests
url  = "http://www.weather.com.cn/data/sk/101010100.html"
response = requests.get(url = url)
response.encoding="utf-8"
# 只有当返回的是json数据才可以使用response.json()来查看json数据,如果返回的不是json数据会报错
data = response.json() # json格式的数据是python的基本类型的数据
print(data)
print("********************************")
print(data['weatherinfo']['city'])

设置请求头:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Richard_Kong
"""
设置请求头
headers = {"Content-Type":"application/json"}
注意:
    当发送请求时填写参数为data json时会自动附加请求头信息
    data: 参数接收form表单数据,后台会自动附加form表单请求信息头(data数据格式为字典)
    json:参数接收json数据,后台会自动附加json表单请求信息头
"""
import requests
login_url = "http://ihrm-test.itheima.net/api/sys/login"
login_headers = {
    "Content_type":"application/json"
}
login_data = {
    "mobile":"13800000002",
    "password":"123456"
}
response = requests.post(url=login_url,json=login_data,headers = login_headers)
print(response.json())

带有验证码的登陆接口如何登陆:

  1、需要先查询验证码接口获取验证码和cookie

  2、获取cookie和验证码后,再携带cookie和验证码 登陆接口

  3、之后的每次请求都要携带 这个cookie,否则服务端无法识别你是谁

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Richard_Kong
"""
带验证码登陆的接口如何处理:
1、要先通过查询验证码接口 获取验证码和cookie信息
2、再通过登陆接口将验证码 和 cookie 信息添加到登陆请求消息中 登陆

TPSHOP的查询验证码请求接口:http://localhost/index.php?m=Home&c=User&a=verify
"""
import requests
verify_url = "http://localhost/index.php?m=Home&c=User&a=verify"
verify_response = requests.get(url=verify_url)
verify_response.encoding = 'utf-8'
print(verify_response.headers) # content-Type 是image 图片格式
PHPSESSID = verify_response.cookies.get("PHPSESSID")
print(verify_response.cookies)
print(PHPSESSID)

login_url = "http://localhost/index.php?m=Home&c=User&a=do_login"
login_data = {
    "username":"15958186974",
    "password":"123456",
    "verify_code":"8888"
}
# 注意 cookie 也是一个key-value对象
login_cookies = {
    "PHPSESSID":PHPSESSID
}
login_response = requests.post(url=login_url,data=login_data,cookies = login_cookies)
print(login_response.json())
# 之后每次发请求 都需要携带cookie,只有携带cookie 服务端才知道 是谁的请求

 使用session对象来保存各请求之间的数据

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Richard_Kong
"""
设置session
作用:
    在多个请求之间存储数据并自动添加数据 如cookie
    使用:
        实例化 session = requests.session()
        发送请求:request.get-->session.get()
session对象会自动保存cookie数据

使用session 来实现tpshop带验证码的登陆
"""
import requests
session = requests.session()
session.get("http://localhost/index.php?m=Home&c=User&a=verify")

login_url = "http://localhost/index.php?m=Home&c=User&a=do_login"
login_data = {
    "username":"15958186974",
    "password":"123456",
    "verify_code":"8888"
}
response = session.post(url=login_url,data=login_data)
print(response.json())

 

posted @ 2021-09-07 23:01  GalaxyStar  阅读(13)  评论(0编辑  收藏  举报