python 写一个对比2个接口的响应数据其中的一个字段
下面是一个使用Python对比两个接口响应数据中一个字段的示例代码:
import requests
def get_data(url):
response = requests.get(url)
data = response.json()
return data
def compare_field(url1, url2, field):
data1 = get_data(url1)
data2 = get_data(url2)
if data1[field] == data2[field]:
print(f"The field '{field}' is the same in both responses.")
else:
print(f"The field '{field}' is different in both responses.")
url1 = "https://api.example.com/endpoint1"
url2 = "https://api.example.com/endpoint2"
field = "field_name"
compare_field(url1, url2, field)
请将上述代码中的url1和url2替换为你要对比的两个接口的URL,field替换为你要对比的字段名。然后运行代码,它将会从这两个接口获取数据并对比指定字段的值,最后输出对比结果。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/i11K 著作权归作者所有。请勿转载和采集!