API学习

API学习

API到底是什么?
Application Programming Interface,应用程序编程接口
站点在推出基于开放API 标准的产品和服务后,无需花费力气做大量的市场推广,只要提供的服务或应用出色易用,其他站点就会主动将开放API 提供的服务整合到自己的应用之中。
也就是网站提供给我们可以获取一些信息的方法,我们可以调用这些方法。
这些请求的数据将以易于处理的格式(如JSON或CSV)返回。

使用API调用请求GitHub数据

在这里插入图片描述

如图所示,在浏览器中输入 https://api.github.com/,响应的数据是GitHub网站提供的所有API,拿第一个来说:current_user_url": "https://api.github.com/user 可以通过这个链接表示获取当前用户的信息,
如下图所示:

在这里插入图片描述
https://api.github.com/user/用户名 即可访问当前用户的信息。

我们来看一个例子:查看GitHub托管了多少个Python项目,还有关于最受欢迎的Python仓库的信息,下面来仔细研究这个调用。

https://api.github.com/search/repositories?q=language:python&sort=stars
第一部分:https://api.github.com 将请求发送到GitHub网站中响应API调用的部分
第二部分:search/repositories 让API搜索GitHub上所有仓库
repositories 后面的问号指出我们要传递一个实参,q表示查询,而等号让我们能够开始指定查询(q=)。通过使用language:python,我们指出只想获得注意语言为python的仓库的信息,最后一部分,sort=stars指定将项目按其获得的星级进行排序。

响应如下:

{
  "total_count": 3585858,
  "incomplete_results": false,
  "items": [
    {
      "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}",
        "received_events_url": "https://api.github.com/users/vinta/received_events",
        "type": "User",
        "site_admin": false
      },
      "html_url": "https://github.com/vinta/awesome-python",
      "description": "A curated list of awesome Python frameworks, libraries, software and resources",
      "fork": false,
      "url": "https://api.github.com/repos/vinta/awesome-python",
      "forks_url": "https://api.github.com/repos/vinta/awesome-python/forks",
      --snip--

如上,该GitHub总共有3585858个python项目,incomplete_results": false表示请求是成功的(它并非不完整,双重否定表肯定),接下来的items包含最受欢迎的python项目的详细信息。

监视API的速率限制:
大多数API都存在速率限制,即在特定时间内可执行的请求数存在限制,要知道是否接近限制,可在浏览器中输入:https://api.github.com/rate_limit
在这里插入图片描述

search中,权限为每分钟10个请求,在当前这一分钟内,我们还可以执行8个请求。

猜你喜欢

转载自blog.csdn.net/weixin_43670105/article/details/88582058