1.get请求
import requests
url = "http://www.baidu.com"
r = requests.get(url)
print("请求url:",r.url)
print("状态码:",r.status_code)
print("文本响应内容:",r.text)
1.1get带参数
import requests
url = "http://www.baidu.com"
params = {"id":1001,"kw":"北京"}
r = requests.get(url,params=params)
print("请求url:",r.url)
print("状态码:",r.status_code)
print("文本响应内容:",r.text)
2.post请求
使用json新增学院 使用data新增学院
3.其他请求类型
4.1响应方法(encoding和headers)
4.2响应方法(cookies和content)
import requests
url = "https://img2.baidu.com/it/u=3973222012,893181252&fm=253&fmt=auto&app=138&f=JPEG?w=650&h=408"
r = requests.get(url)
with open("./baidu.png","wb") as f:
f.write(r.content)
5.案例执行练练手
tp商城测试网址 验证码:1 步骤: 1.获取相关数据 1.1、获取验证码:http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=verify 1.2、登入接口:http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=do_login 1.3、订单:http://www.testingedu.com.cn:8000/Home/User/index.html 1.4、登入数据: data ={ “username”: “13800138006”, “password”: “123456”, “verify_code”: 1 }
"""
1.1、获取验证码:http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=verify
1.2、登入接口:http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=do_login
1.3、订单:http://www.testingedu.com.cn:8000/Home/User/index.html
1.4、登入数据: data = { "username": "13800138006",
"password": "123456",
"verify_code": 1
}
"""
import requests
url_login = "http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=do_login"
data = {"username": "13800138006",
"password": "123456",
"verify_code": 1
}
r = requests.post(url=url_login,data=data)
print(r.json())
登入过程中提示验证码错误 原因: 由图可以看出我们data数据里的验证码没有cookie信息,所以提示验证码错误
改正后代码如下: ps:登入成功及成功后执行get请求需要都添加cookies值
"""
1.1、获取验证码:http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=verify
1.2、登入接口:http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=do_login
1.3、订单:http://www.testingedu.com.cn:8000/Home/Order/order_list.html
1.4、登入数据: data = {"username": "13800138006",
"password": "123456",
"verify_code": 1
}
"""
import requests
url_verify_code = "http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=verify"
r = requests.get(url=url_verify_code)
print(r.cookies)
r_cookies = {"PHPSESSID":r.cookies['PHPSESSID']}
url_login = "http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=do_login"
data = {"username": "13800138006",
"password": "123456",
"verify_code": 1
}
r = requests.post(url=url_login,data=data,cookies=r_cookies)
print(r.json())
url_user = "http://www.testingedu.com.cn:8000/Home/Order/order_list.html"
r = requests.get(url=url_user,cookies=r_cookies)
print(r.text)
6.session应用
在第5步案例执行发现如果用cookies需要登入及登入时添加cookies信息比较繁琐,可以用session替代 代码如下:
"""
1.1、获取验证码:http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=verify
1.2、登入接口:http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=do_login
1.3、订单:http://www.testingedu.com.cn:8000/Home/Order/order_list.html
1.4、登入数据: data = { "username": "13800138006",
"password": "123456",
"verify_code": 1
}
"""
import requests
session = requests.session()
url_verify = "http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=verify"
session.get(url_verify)
url_login = "http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=do_login"
data = {"username": "13800138006",
"password": "123456",
"verify_code": 1
}
r = session.post(url=url_login,data=data)
print(r.json())
url_user = "http://www.testingedu.com.cn:8000/Home/Order/order_list.html"
r = session.get(url_user)
print(r.text)
7.unittest框架及requests
具体框架:
"""
1.1、获取验证码:http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=verify
1.2、登入接口:http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=do_login
1.3、订单:http://www.testingedu.com.cn:8000/Home/Order/order_list.html
1.4、登入数据: data = { "username": "13800138006",
"password": "123456",
"verify_code": 1
}
"""
import unittest
import requests
class TestLogin(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_login_success(self):
pass
def test_username_not_exist(self):
pass
def test_password_error(self):
pass
具体代码: 需要继承unittest.TestCase,后面每一个以test开头的函数都会被当成一条用例
"""
1.1、获取验证码:http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=verify
1.2、登入接口:http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=do_login
1.3、订单:http://www.testingedu.com.cn:8000/Home/Order/order_list.html
1.4、登入数据: data = { "username": "13800138006",
"password": "123456",
"verify_code": 1
}
"""
import unittest
import requests
class TshopLogin(unittest.TestCase):
def setUp(self):
self.session = requests.session()
self.url_login = "http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=do_login"
self.url_verify_code = "http://www.testingedu.com.cn:8000/index.php?m=Home&c=User&a=verify"
def tearDown(self):
self.session.close()
def test_login_success(self):
self.session.get(self.url_verify_code)
data = {"username": "13800138006",
"password": "123456",
"verify_code": 1
}
r = self.session.post(self.url_login,data=data)
try:
self.assertEqual("登陆成功",r.json()['msg'])
except AssertionError as e:
print(e)
def test_username_not_exist(self):
self.session.get(self.url_verify_code)
data = {"username": "138001380061",
"password": "123456",
"verify_code": 1
}
r = self.session.post(self.url_login, data=data)
try:
self.assertEqual("账号不存在!", r.json()['msg'])
except AssertionError as e:
print(e)
def test_password_error(self):
self.session.get(self.url_verify_code)
data = {"username": "13800138006",
"password": "12345",
"verify_code": 1
}
r = self.session.post(self.url_login, data=data)
try:
self.assertEqual("密码错误!", r.json()['msg'])
except AssertionError as e:
print(e)
if __name__ == '__main__':
unittest.main()
|