Python基础API的调用

来自linux中国网wiki
Evan讨论 | 贡献2024年10月23日 (三) 13:29的版本 →‎使用requests库发送GET请求
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

一直在用 居然没注意到这个就叫api调用

使用requests库发送GET请求

也有 requests.post
info = requests.post(url=webhook, data=message_json, headers=header).json() dingding机器人
➜  tmp cat gapi.sh 
import requests 
response = requests.get('https://api.github.com/users/octocat')

if response.status_code == 200:
  user_data = response.json()
  print(user_data)

else:
  print("failed to retrieve data: {response.status_code}")
➜  tmp py3  gapi.sh
{'login': 'octocat', 'id': 583231, 'node_id': 'MDQ6VXNlcjU4MzIzMQ==', 'avatar_url': 'https://avatars.githubusercontent.com/u/583231?v=4', 'gravatar_id': '', 'url': 'https://api.github.com/users/octocat', 'html_url': 'https://github.com/octocat', 'followers_url': 'https://api.github.com/users/octocat/followers', 'following_url': 'https://api.github.com/users/octocat/following{/other_user}', 'gists_url': 'https://api.github.com/users/octocat/gists{/gist_id}', 'starred_url': 'https://api.github.com/users/octocat/starred{/owner}{/repo}', 'subscriptions_url': 'https://api.github.com/users/octocat/subscriptions', 'organizations_url': 'https://api.github.com/users/octocat/orgs', 'repos_url': 'https://api.github.com/users/octocat/repos', 'events_url': 'https://api.github.com/users/octocat/events{/privacy}', 'received_events_url': 'https://api.github.com/users/octocat/received_events', 'type': 'User', 'site_admin': False, 'name': 'The Octocat', 'company': '@github', 'blog': 'https://github.blog', 'location': 'San Francisco', 'email': None, 'hireable': None, 'bio': None, 'twitter_username': None, 'public_repos': 8, 'public_gists': 8, 'followers': 15135, 'following': 9, 'created_at': '2011-01-25T18:44:36Z', 'updated_at': '2024-09-22T11:25:27Z'}
➜  tmp 


references

如何调用api接口python