给我一段调用api接口的代码,python实现
时间: 2023-05-21 12:03:03 浏览: 80
当然可以,以下是一个简单的 Python 代码示例,用于调用一个名为 "example_api" 的 API 接口:
```python
import requests
url = "https://example.com/api/example_api"
headers = {"Authorization": "Bearer YOUR_ACCESS_TOKEN"}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
print(data)
else:
print("Failed to call API: ", response.status_code)
```
请注意,您需要将 "YOUR_ACCESS_TOKEN" 替换为您的实际访问令牌,以便成功调用 API 接口。
相关问题
给我一段调用chatgpt接口的Python代码
这是一段可以用来调用ChatGPT接口的Python代码:import requestsparams = {
'prompt': '你好,ChatGPT!',
'length': 10,
'temperature': 0.7,
'top_p': 0.9,
'n': 5
}r = requests.get('https://api.openai.com/v1/engines/chatGPT/completions', params=params, headers={'Authorization': 'Bearer <INSERT_YOUR_API_KEY_HERE>'})print(r.json()['choices'][0]['text'])
给我写一段调用百度接口的python脚本
这是一段简单的调用百度API的Python脚本:import requestsurl = 'http://openapi.baidu.com/'
params = {
'key' : 'xxxxxxxxx',
'q' : '查询内容',
'ie' : 'utf-8'
}r = requests.get(url, params=params)if r.status_code == 200:
print(r.json())