Python 中的 API 入门
Chris Ramakers
Engineering Manager
http://350.5th-ave.com/unit/243

# 将查询参数直接拼接到 URL 字符串
response = requests.get('http://350.5th-ave.com/unit/243?floor=77&elevator=True')
print(response.url)
http://350.5th-ave.com/unit/243?floor=77&elevator=True
params 参数添加查询参数# 创建字典
query_params = {'floor': 77, 'elevator': True}
# 通过 `params` 参数传入字典
response = requests.get('http://350.5th-ave.com/unit/243', params=query_params)
print(response.url)
http://350.5th-ave.com/unit/243?floor=77&elevator=True
http://350.5th-ave.com/unit/243| Verb | Action | Description |
|---|---|---|
| GET | Read | 查看邮箱内容 |
| POST | Create | 投递新包裹到邮箱 |
| PUT | Update | 用新包裹替换所有包裹 |
| DELETE | Delete | 清空邮箱中的包裹 |
# GET = 获取资源 response = requests.get('http://350.5th-ave.com/unit/243')# POST = 创建资源 response = requests.post('http://350.5th-ave.com/unit/243', data={"key": "value"}) # PUT = 更新现有资源 response = requests.put('http://350.5th-ave.com/unit/243', data={"key": "value"})# DELETE = 删除资源 response = requests.delete('http://350.5th-ave.com/unit/243')
requests 包中都有对应方法data 参数向 POST 或 PUT 请求传递数据。Python 中的 API 入门