写一段调用网页接口的python程序
以下是一个使用Python调用网页接口的示例程序:
import requests
# 设置接口地址
url = "https://api.example.com/get_data"
# 设置请求参数
params = {
"name": "Alice",
"age": 25
}
# 发送GET请求
response = requests.get(url, params=params)
# 解析响应结果
if response.status_code == 200:
data = response.json()
print(data)
else:
print("请求失败,错误码:", response.status_code)
在这个示例程序中,我们使用了Python的requests库来发送GET请求获取数据。我们首先设置了接口地址和请求参数,然后使用requests.get()方法发送请求并获取响应结果。最后,我们对响应结果进行了解析并输出了结果或错误码。
原文地址: https://www.cveoy.top/t/topic/c3Vg 著作权归作者所有。请勿转载和采集!