1 使用api调用数据:
在浏览器的地址栏中输入:
https://api.github.com/search/repositories?q=language:python&sort=starts
这个api调用github当前托管的python项目
{ "total_count": 3872430, "incomplete_results": true, "items": [ { "id": 21289110, "node_id": "MDEwOlJlcG9zaXRvcnkyMTI4OTExMA==", "name": "awesome-python", "full_name": "vinta/awesome-python", "private": false, ---snip----
"total_count": github上共有的python项目
"incomplete_results": true, 表示请求成功,如果github无法处理该api他返回的值是true
"items"python项目的具体的信息
2 安装requests,
requests 可以让python程序轻松向网站请求信息以及检查返回的响应
pip install --user requests
代表仅该用户的安装,安装后仅该用户可用。处于安全考虑,尽量使用该命令进行安装。
3 简单的处理api的响应
import requests url = 'https://api.github.com/search/repositories?q=language:python&sort=starts' r = requests.get(url) # 打印出状态码 print('status code',r.status_code) # status code 200 转态码 200表示成功 response_dict = r.json() # 打印出所有的keys print(response_dict.keys()) # dict_keys(['total_count', 'incomplete_results', 'items'])
4仓库的信息
仓库的信息:
{ "id": 21289110, "node_id": "MDEwOlJlcG9zaXRvcnkyMTI4OTExMA==", "name": "awesome-python", "full_name": "vinta/awesome-python", "private": false, "owner": { "login": "vinta", "id": 652070, "node_id": "MDQ6VXNlcjY1MjA3MA==", "avatar_url": "https://avatars2.githubusercontent.com/u/652070?v=4", "gravatar_id": "", "url": "https://api.github.com/users/vinta", "html_url": "https://github.com/vinta", "followers_url": "https://api.github.com/users/vinta/followers", "following_url": "https://api.github.com/users/vinta/following{/other_user}", "gists_url": "https://api.github.com/users/vinta/gists{/gist_id}", "starred_url": "https://api.github.com/users/vinta/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/vinta/subscriptions", "organizations_url": "https://api.github.com/users/vinta/orgs", "repos_url": "https://api.github.com/users/vinta/repos", "events_url": "https://api.github.com/users/vinta/events{/privacy}"