使用Web API_&_使用Pygal可视化仓库

1.Web API

Web API是网站的一部分,用于与使用非常具体的URL请求特定信息的程序交互,这种请求称为API调用。

请求的数据将以易于处理的格式(如JSON或CSV)返回,依赖于外部数据的大多数程序都依赖于API调用,如集成社交媒体网站的应用程序。

在浏览器中输入如下地址并按回车:

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

下面显示了响应的前几行:

{
  "total_count": 2639356,
  "incomplete_results": false,
  "items": [
    {
      "id": 21289110,
      "name": "awesome-python",
      "full_name": "vinta/awesome-python",
      --snip--

可知GitHub共有2639356个Python项目,"incomplete_results"的值为false,据此我们知道请求是成功的。

2.处理API响应&概述最受欢迎仓库

import requests

url='https://api.github.com/search/repositories?q=language:python&sort=stars'
r=requests.get(url)
print("Status code:",r.status_code)

response_dict=r.json()
print("Total repositories:",response_dict['total_count'])

repo_dicts=response_dict['items']
print("Repositories returned:",len(repo_dicts))

print("Selected information about each repository:")
for repo_dict in repo_dicts:
    print('\nName:',repo_dict['name'])
    print('Owner:',repo_dict['owner']['login'])
    print('Stars:',repo_dict['stargazers_count'])
    print('Repository:',repo_dict['html_url'])
    print('Created:',repo_dict['created_at'])
    print('Description:',repo_dict['description'])

状态码200表示请求成功,循环调用每个仓库的特定信息,以便能够在可视化中包含这些信息。

3.监视API的速率限制

https://api.github.com/rate_limit

可知极限为执行10个请求,我们还可以执行8个请求,如果获得API密钥后,配额将高得多。

猜你喜欢

转载自www.cnblogs.com/exciting/p/9048530.html