第一次写接口自动化登录测试时,就遇到这个问题,TypeError: a bytes-like object is required, not 'str'
因为python基础不扎实的原因导致的,所以就想能不能将这个bytes数据类型转换成str类型不就好了
那就试着转换呗
于是就百度喽,参考如下http://blog.csdn.net/bible_reader/article/details/53047550
于是就用了一个简单的方法
r = r.content.decode()
这样就可以了
还是附上这次代码吧,给自己一个教训,这是我登录某网站的一个接口测试,很基础,请大家多多指教
#coding:utf-8
#登录函数
def login(username,pwd):
"""登录函数"""
import requests
# 第一步 登录
login_url = "https:*******"
header = {
# 登录抓包获取的头部
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
"Accept": "*/*;q=0.5, text/javascript, application/javascript, application/ecmascript, application/x-ecmascript",
"Origin": "https://testerhome.com",
"X-CSRF-Token": "XTd8APQgKE4amqtZWpNhszEmUeoCyemfMh5qF7Vm1YT9bYUNYnxin9OjLifmCiafmVuvj4gVDteQV1qebjXUsQ==",
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9"
}
body1 = {
"utf8": "✓",
"user[login]": username,
"user[password]": pwd,
"user[remember_me]": "0",
"commit": "登录"
}
s = requests.session()
r = s.post(url=login_url, data=body1, headers=header, verify=False)
# 查看登录是否成功
print(type(r.content))
r = r.content.decode()
return r
def is_login_sucess(res):
if "帐号或密码错误" in res:
return False
elif "Turbolinks.visit" in res:
return True
else:
return False
if __name__=="__main__":
#验证登录是否成功
username = "*****",
pwd = "P*******",
a1=login(username,pwd)
#print(a1)
print(is_login_sucess(a1))