- 聚合天气API测试

import requests
import unittest
class APITest(unittest.TestCase):
def setUp(self):
print("开始")
def tearDown(self):
print("结束")
def test01(self):
url = "http://v.juhe.cn/weather/index"
para = {"cityname":"南京","key":"d51144a671371d315978aa08493bf86c "}
res = requests.get(url,params = para)
info = res.json()
self.assertEqual(info["reason"],"successed!")
if __name__ == "__main__":
unittest.main()

2. 快递网站API测试
a 在快递查询网站中输入快递单号:

使用fiddler抓包
import requests
import unittest
class APITest(unittest.TestCase):
def setUp(self):
print("开始")
def tearDown(self):
print("结束")
def test01(self):
url = "https://www.kuaidi100.com/query?type=????&postid=???1&temp???&phone="
res = requests.get(url,verify = False)
info = res.json()
print(info)
if __name__ == "__main__":
unittest.main()

注:
1)如果运行程序时,fiddler处于开启状态中那么要将get请求中的verify设置为False(默认不写时值为True),不然会报一个CERTIFICATE_VERIFY_FAILED _ssl.c:1108的错。
2)如果要看json内容,那么要先查看抓取的反馈中有没有json内容,如果没有会报JsonDecodeError错误。
本文通过两个实例,详细展示了如何使用Python的requests库进行API测试,包括聚合天气API和快递查询API的测试过程及常见问题解决方案。
1923

被折叠的 条评论
为什么被折叠?



