Github API Pthon请求

使用Web应用编程接口(API)自动请求网站的特定信息而不是整个网页,再对这些信息进行可视化,web API是网站的一部分,用于与使用非常具体的URL请求特定信息的程序交互。这种请求称为API调用。请求的数据将以易于处理的格式(如JSON或CSV)返回。依赖于外部数据源的大多数应用程序都依赖于API 调用,如集成社交媒体网站的应用程序。
参考网址 https://blog.csdn.net/weixin_40575956/article/details/80148472

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响应存储在一个变量中
# print(r.text)
# print(type(r.text))
response_dict = r.json()
# # 处理结果
print(response_dict.keys())
# print(type(response_dict))
print("Total repositories:", response_dict['total_count'])
# 探索有关仓库的信息
repo_dicts = response_dict['items']
# print(repo_dicts)
print("Repositories returned:", len(repo_dicts))
# 研究有关仓库的信息
repo_dict = repo_dicts[0]
print('\nkeys:', len(repo_dict))
i = 1
for key in sorted(repo_dict.keys()):
    print(i, key, sep=" : ")
    i += 1
print("\nSelected information about first repository")
print("name:",repo_dict['name'])
print("owner:", repo_dict['owner']['login'])
print("start:", repo_dict['stargazers_count'])
print("Repository:", repo_dict['html_url'])
print("Created:", repo_dict["created_at"])
print("Updated:", repo_dict['updated_at'])
print("Description:", repo_dict['description'])

print("\nSelected information about each repository")
k = 1
for repo_dict in repo_dicts:
    print("\n 第%d个仓库"%k)
    print("name:", repo_dict['name'])
    print("owner:", repo_dict['owner']['login'])
    print("start:", repo_dict['stargazers_count'])
    print("Repository:", repo_dict['html_url'])
    print("Created:", repo_dict["created_at"])
    print("Updated:", repo_dict['updated_at'])
    print("Description:", repo_dict['description'])
    k += 1
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • Spring Web MVC Spring Web MVC 是包含在 Spring 框架中的 Web 框架,建立于...
    Hsinwong阅读 22,545评论 1 92
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,292评论 25 708
  • 我和老公在3年前认识,那时的我刚从南方回到山东,漂泊了5年之后,想要回到家乡,靠近家人,体会亲情的温暖,然后在机缘...
    涵娅T阅读 264评论 0 0
  • 前两天听一个朋友吐槽,他说坚持了21天早上四点起床,结果第22天被打回了原形。 我特别好奇地问他,为什么你一定要四...
    若扶风阅读 324评论 0 0