Python入门到实践——项目2(第17章 使用API)

17.1.2  使用 API 调用请求数据 
 

GitHub 的 API 让你能够通过 API 调用来请求各种信息。要知道 API 调用是什么样的,请在浏览器的地址栏中输入如下地址并按回车键:

https://api.github.com/search/repositories?q=language:python&sort=stars

这个调用返回 GitHub 当前托管了多少个 Python 项目,还有有关最受欢迎的 Python 仓库的信息。下面来仔细研究这个调用。

第一部分:

( https://api.github.com/ )将请求发送到 GitHub 网站中响应 API 调用的部分;

第二部分:

( search/repositories )让 API 搜索 GitHub 上的所有仓库。

第三部分:

(?q=language:python&sort=stars),repositories 后面的问号指出我们要传递一个实参。 q 表示查询,而等号让我们能够开始指定查询( q= )。通过使用 language:python ,我们指出只想获取主要语言为Python 的仓库的信息。最后一部分( &sort=stars )指定将项目按其获得的星级进行排序。下面显示了响应的前几行。从响应可知,该 URL 并不适合人工输入。

{
  "total_count": 2430749,
  "incomplete_results": false,
  "items": [
    {
      "id": 21289110,
      "name": "awesome-python",
      "full_name": "vinta/awesome-python",
      "owner": {
        "login": "vinta",
        "id": 652070,
        "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}",
        "received_events_url": "https://api.github.com/users/vinta/received_events",
        "type": "User",
        "site_admin": false
      },

  GitHub 总共有 2430749 个 Python 项目。 "incomplete_results" 的值为 false ,据此我们知道请求是成功的(它并非不完整的)。倘若 GitHub无法全面处理该 API ,它返回的这个值将为 true 。接下来的列表中显示了返回的 "items" ,其中包含 GitHub 上最受欢迎的 Python 项目的详细信息。

17.1.4  处理 API 响应
下面来编写一个程序,它执行 API 调用并处理结果,找出 GitHub 上星级最高的 Python 项目:
python_repos.py

import requests   

#执行API调用并存储响应
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
r = requests.get(url)   #requests 来执行调用
print("Status code :", r.status_code)

#将API响应存储在一个变量中
response_dict = r.json()   #这个r是上面存储调用url信息的r

#处理结果
print(response_dict.keys())

我们导入了模块 requests 。我们存储 API 调用的 URL ,然后使用 requests 来执行调用。我们调用 get() 并将 URL 传递给它,再将响应对象存储在变量 r 中。响应对象包含一个名为 status_code 的属性,它让我们知道请求是否成功了(状态码 200 表示请求成功)。我们打印 status_code ,核实调用是否成功了。
这个 API 返回 JSON 格式的信息,因此我们使用方法 json() 将这些信息转换为一个 Python 字典。我们将转换得到的字典存储在 response_dict 中。
最后,我们打印 response_dict 中的键。输出如下:
Status code : 200
dict_keys(['total_count', 'incomplete_results', 'items'])

>>> 

状态码为 200 ,因此我们知道请求成功了。响应字典只包含三个键: 'items' 、 'total_count' 和 'incomplete_results' 。

17.1.5  处理响应字典
将 API 调用返回的信息存储到字典中后,就可以处理这个字典中的数据了。下面来生成一些概述这些信息的输出。这是一种不错的方式,可确认收到了期望的信息,进而可以开始研究感兴趣的信息:
python_repos.py

import requests

#执行API调用并存储响应
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
r = requests.get(url)
print("Status code :", r.status_code)

#将API响应存储在一个变量中
response_dict = r.json()
print("Total repositories: ", response_dict['total_count'])

#探索有关仓库的信息
repo_dicts = response_dict['items']
print("Repositories returened: ", len(repo_dicts))

#研究第一个仓库
repo_dict = repo_dict[0]
print("\nKeys:", len(repo_dict))
for key in sorted(repo_dict.keys()):
    print(key)

#处理结果
print(response_dict.keys())
下面来提取 repo_dict 中与一些键相关联的值:

猜你喜欢

转载自blog.csdn.net/zhuoyue65/article/details/79519542