# 昨日知识点回顾
json文件提取数据、绘制图表渐变色显示
# 今日知识点学习
第17章
17.1 使用Web API
Web API作为网站的一部分,用于与使用具体URL请求特定信息的程序交互,这种请求称为API调用。
17.1.1 Git 和 GitHub
Git:分布式版本控制系统,帮助人们管理为项目所做的工作,避免一个人所做的影响其他人所做的修改(类似于共享文档)。在项目实现新功能时,Git跟踪你对每个文件所做的修改,确认代码可行后,提交上线项目新功能。如果犯错可撤回,可以返回之前任何可行状态。
GitHub:让程序员可以协助开发项目的网站。
17.1.2 使用API调用请求数据
浏览器地址栏输入:https://api.github.com/search/repositories?q=language:python&sort=stars
显示结果:
{"total_count": 14026425,"incomplete_results": true,"items": [{"id": 54346799,"node_id": "MDEwOlJlcG9zaXRvcnk1NDM0Njc5OQ==","name": "public-apis","full_name": "public-apis/public-apis","private": false,"owner": {"login": "public-apis","id": 51121562,"node_id": "MDEyOk9yZ2FuaXphdGlvbjUxMTIxNTYy","avatar_url": "https://avatars.githubusercontent.com/u/51121562?v=4","gravatar_id": "","url": "https://api.github.com/users/public-apis","html_url": "https://github.com/public-apis","followers_url": "https://api.github.com/users/public-apis/followers","following_url": "https://api.github.com/users/public-apis/following{/other_user}","gists_url": "https://api.github.com/users/public-apis/gists{/gist_id}","starred_url": "https://api.github.com/users/public-apis/starred{/owner}{/repo}","subscriptions_url": "https://api.github.com/users/public-apis/subscriptions","organizations_url": "https://api.github.com/users/public-apis/orgs","repos_url": "https://api.github.com/users/public-apis/repos","events_url": "https://api.github.com/users/public-apis/events{/privacy}","received_events_url": "https://api.github.com/users/public-apis/received_events","type": "Organization","site_admin": false},"html_url": "https://github.com/public-apis/public-apis","description": "A collective list of free APIs",
---snip---
17.1.3 安装Requests
17.1.4 处理API响应
import requests# 执行API调用并存储响应
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
# 指定headers显示地要求使用这个版本的API
headers = {'Accept': 'application/vnd.github.v3+json'}
# requests调用API
r = requests.get(url, headers=headers)
# 状态码200表示请求成功
print(f"Status code:{r.status_code}")
# 将API响应赋给一个变量
response_dict = r.json()# 处理结果
print(response_dict.keys())# 运行结果:
# Status code:200
# dict_keys(['total_count', 'incomplete_results', 'items'])
17.1.5 处理响应字典
import requests# 执行API调用并存储响应
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f"Status code:{r.status_code}")
# 将API响应赋给一个变量
response_dict = r.json()
print(f"Total repositories:{response_dict['total_count']}")# 探索有关仓库的信息
repo_dicts = response_dict['items']
print(f"Repositories returned:{len(repo_dicts)}")# 研究第一个仓库
repo_dict = repo_dicts[0]
print(f"\nKeys:{len(repo_dict)}")
for key in sorted(repo_dict.keys()):print(key)# 运行结果:
# Status code:200
# Total repositories:14400151
# Repositories returned:30
#
# Keys:80
# allow_forking
# archive_url
# archived
# assignees_url
# blobs_url
# branches_url
# clone_url
# collaborators_url
# comments_url
# commits_url
# compare_url
# contents_url
# contributors_url
# created_at
# default_branch
# deployments_url
# description
# disabled
# downloads_url
# events_url
# fork
# forks
# forks_count
# forks_url
# full_name
# git_commits_url
# git_refs_url
# git_tags_url
# git_url
# has_discussions
# has_downloads
# has_issues
# has_pages
# has_projects
# has_wiki
# homepage
# hooks_url
# html_url
# id
# is_template
# issue_comment_url
# issue_events_url
# issues_url
# keys_url
# labels_url
# language
# languages_url
# license
# merges_url
# milestones_url
# mirror_url
# name
# node_id
# notifications_url
# open_issues
# open_issues_count
# owner
# private
# pulls_url
# pushed_at
# releases_url
# score
# size
# ssh_url
# stargazers_count
# stargazers_url
# statuses_url
# subscribers_url
# subscription_url
# svn_url
# tags_url
# teams_url
# topics
# trees_url
# updated_at
# url
# visibility
# watchers
# watchers_count
# web_commit_signoff_required
import requests# 执行API调用并存储响应
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f"Status code:{r.status_code}")
# 将API响应赋给一个变量
response_dict = r.json()
print(f"Total repositories:{response_dict['total_count']}")# 探索有关仓库的信息
repo_dicts = response_dict['items']
print(f"Repositories returned:{len(repo_dicts)}")# 研究第一个仓库
repo_dict = repo_dicts[0]print("\nSelected information about first respository:")
print(f"Name:{repo_dict['name']}")
print(f"Owner:{repo_dict['owner']['login']}")
print(f"Stars:{repo_dict['stargazers_count']}")
print(f"Resitory:{repo_dict['html_url']}")
print(f"Created:{repo_dict['created_at']}")
print(f"Updated:{repo_dict['updated_at']}")
print(f"Description:{repo_dict['description']}")# 运行结果:
# Status code:200
# Total repositories:11466073
# Repositories returned:30
#
# Selected information about first respository:
# Name:docopt
# Owner:docopt
# Stars:7891
# Resitory:https://github.com/docopt/docopt
# Created:2012-04-07T17:45:14Z
# Updated:2024-05-15T15:47:30Z
# Description:This project is no longer maintained. Please see https://github.com/jazzband/docopt-ng
17.1.6 概述最受欢迎的仓库
import requests# 执行API调用并存储响应
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
headers = {'Accept': 'application/vnd.github.v3+json'}
r = requests.get(url, headers=headers)
print(f"Status code:{r.status_code}")
# 将API响应赋给一个变量
response_dict = r.json()
print(f"Total repositories:{response_dict['total_count']}")# 探索有关仓库的信息
repo_dicts = response_dict['items']
print(f"Repositories returned:{len(repo_dicts)}")# 研究第一个仓库
repo_dict = repo_dicts[0]print("\nSelected information about each respository:")
for repo_dict in repo_dicts:print(f"\nName:{repo_dict['name']}")print(f"Owner:{repo_dict['owner']['login']}")print(f"Stars:{repo_dict['stargazers_count']}")print(f"Resitory:{repo_dict['html_url']}")print(f"Created:{repo_dict['created_at']}")print(f"Updated:{repo_dict['updated_at']}")print(f"Description:{repo_dict['description']}")# 运行结果:
# Status code:200
# Total repositories:13942109
# Repositories returned:30
#
# Selected information about each respository:
#
# Name:public-apis
# Owner:public-apis
# Stars:294054
# Resitory:https://github.com/public-apis/public-apis
# Created:2016-03-20T23:49:42Z
# Updated:2024-05-20T13:09:53Z
# Description:A collective list of free APIs
#
# Name:system-design-primer
# Owner:donnemartin
# Stars:257751
# Resitory:https://github.com/donnemartin/system-design-primer
# Created:2017-02-26T16:15:28Z
# Updated:2024-05-20T13:13:05Z
# Description:Learn how to design large-scale systems. Prep for the system design interview. Includes Anki flashcards.
#
# Name:stable-diffusion-webui
# Owner:AUTOMATIC1111
# Stars:131594
# Resitory:https://github.com/AUTOMATIC1111/stable-diffusion-webui
# Created:2022-08-22T14:05:26Z
# Updated:2024-05-20T12:57:38Z
# Description:Stable Diffusion web UI
#
# Name:thefuck
# Owner:nvbn
# Stars:83115
# Resitory:https://github.com/nvbn/thefuck
# Created:2015-04-08T15:08:04Z
# Updated:2024-05-20T13:10:19Z
# Description:Magnificent app which corrects your previous console command.
#
# Name:yt-dlp
# Owner:yt-dlp
# Stars:72475
# Resitory:https://github.com/yt-dlp/yt-dlp
# Created:2020-10-26T04:22:55Z
# Updated:2024-05-20T13:10:53Z
# Description:A feature-rich command-line audio/video downloader
#
# Name:fastapi
# Owner:tiangolo
# Stars:71670
# Resitory:https://github.com/tiangolo/fastapi
# Created:2018-12-08T08:21:47Z
# Updated:2024-05-20T11:32:01Z
# Description:FastAPI framework, high performance, easy to learn, fast to code, ready for production
#
# Name:devops-exercises
# Owner:bregman-arie
# Stars:63948
# Resitory:https://github.com/bregman-arie/devops-exercises
# Created:2019-10-03T17:31:21Z
# Updated:2024-05-20T12:42:03Z
# Description:Linux, Jenkins, AWS, SRE, Prometheus, Docker, Python, Ansible, Git, Kubernetes, Terraform, OpenStack, SQL, NoSQL, Azure, GCP, DNS, Elastic, Network, Virtualization. DevOps Interview Questions
#
# Name:awesome-machine-learning
# Owner:josephmisiti
# Stars:63846
# Resitory:https://github.com/josephmisiti/awesome-machine-learning
# Created:2014-07-15T19:11:19Z
# Updated:2024-05-20T11:18:56Z
# Description:A curated list of awesome Machine Learning frameworks, libraries and software.
#
# Name:whisper
# Owner:openai
# Stars:61613
# Resitory:https://github.com/openai/whisper
# Created:2022-09-16T20:02:54Z
# Updated:2024-05-20T13:03:43Z
# Description:Robust Speech Recognition via Large-Scale Weak Supervision
#
# Name:scikit-learn
# Owner:scikit-learn
# Stars:58360
# Resitory:https://github.com/scikit-learn/scikit-learn
# Created:2010-08-17T09:43:38Z
# Updated:2024-05-20T12:36:35Z
# Description:scikit-learn: machine learning in Python
#
# Name:d2l-zh
# Owner:d2l-ai
# Stars:57414
# Resitory:https://github.com/d2l-ai/d2l-zh
# Created:2017-08-23T04:40:24Z
# Updated:2024-05-20T13:06:17Z
# Description:《动手学深度学习》:面向中文读者、能运行、可讨论。中英文版被70多个国家的500多所大学用于教学。
#
# Name:screenshot-to-code
# Owner:abi
# Stars:51407
# Resitory:https://github.com/abi/screenshot-to-code
# Created:2023-11-14T17:53:32Z
# Updated:2024-05-20T13:04:16Z
# Description:Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)
#
# Name:scrapy
# Owner:scrapy
# Stars:51177
# Resitory:https://github.com/scrapy/scrapy
# Created:2010-02-22T02:01:14Z
# Updated:2024-05-20T12:33:49Z
# Description:Scrapy, a fast high-level web crawling & scraping framework for Python.
#
# Name:Real-Time-Voice-Cloning
# Owner:CorentinJ
# Stars:51004
# Resitory:https://github.com/CorentinJ/Real-Time-Voice-Cloning
# Created:2019-05-26T08:56:15Z
# Updated:2024-05-20T11:48:51Z
# Description:Clone a voice in 5 seconds to generate arbitrary speech in real-time
#
# Name:gpt-engineer
# Owner:gpt-engineer-org
# Stars:50790
# Resitory:https://github.com/gpt-engineer-org/gpt-engineer
# Created:2023-04-29T12:52:15Z
# Updated:2024-05-20T12:30:08Z
# Description:Specify what you want it to build, the AI asks for clarification, and then builds it.
#
# Name:faceswap
# Owner:deepfakes
# Stars:49464
# Resitory:https://github.com/deepfakes/faceswap
# Created:2017-12-19T09:44:13Z
# Updated:2024-05-20T12:38:10Z
# Description:Deepfakes Software For All
#
# Name:grok-1
# Owner:xai-org
# Stars:48512
# Resitory:https://github.com/xai-org/grok-1
# Created:2024-03-17T08:53:38Z
# Updated:2024-05-20T13:01:48Z
# Description:Grok open release
#
# Name:yolov5
# Owner:ultralytics
# Stars:47467
# Resitory:https://github.com/ultralytics/yolov5
# Created:2020-05-18T03:45:11Z
# Updated:2024-05-20T12:39:12Z
# Description:YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
#
# Name:DeepFaceLab
# Owner:iperov
# Stars:45740
# Resitory:https://github.com/iperov/DeepFaceLab
# Created:2018-06-04T13:10:00Z
# Updated:2024-05-20T12:37:54Z
# Description:DeepFaceLab is the leading software for creating deepfakes.
#
# Name:professional-programming
# Owner:charlax
# Stars:45462
# Resitory:https://github.com/charlax/professional-programming
# Created:2015-11-07T05:07:52Z
# Updated:2024-05-20T12:48:24Z
# Description:A collection of learning resources for curious software engineers
#
# Name:hackingtool
# Owner:Z4nzu
# Stars:43007
# Resitory:https://github.com/Z4nzu/hackingtool
# Created:2020-04-11T09:21:31Z
# Updated:2024-05-20T12:51:24Z
# Description:ALL IN ONE Hacking Tool For Hackers
#
# Name:MetaGPT
# Owner:geekan
# Stars:40070
# Resitory:https://github.com/geekan/MetaGPT
# Created:2023-06-30T09:04:55Z
# Updated:2024-05-20T12:29:43Z
# Description:🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
#
# Name:python-patterns
# Owner:faif
# Stars:39544
# Resitory:https://github.com/faif/python-patterns
# Created:2012-06-06T21:02:35Z
# Updated:2024-05-20T11:12:15Z
# Description:A collection of design patterns/idioms in Python
#
# Name:PaddleOCR
# Owner:PaddlePaddle
# Stars:39051
# Resitory:https://github.com/PaddlePaddle/PaddleOCR
# Created:2020-05-08T10:38:16Z
# Updated:2024-05-20T13:11:14Z
# Description:Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)
#
# Name:Deep-Learning-Papers-Reading-Roadmap
# Owner:floodsung
# Stars:37466
# Resitory:https://github.com/floodsung/Deep-Learning-Papers-Reading-Roadmap
# Created:2016-10-14T11:49:48Z
# Updated:2024-05-20T12:15:58Z
# Description:Deep Learning papers reading roadmap for anyone who are eager to learn this amazing tech!
#
# Name:text-generation-webui
# Owner:oobabooga
# Stars:37072
# Resitory:https://github.com/oobabooga/text-generation-webui
# Created:2022-12-21T04:17:37Z
# Updated:2024-05-20T12:37:44Z
# Description:A Gradio web UI for Large Language Models. Supports transformers, GPTQ, AWQ, EXL2, llama.cpp (GGUF), Llama models.
#
# Name:stablediffusion
# Owner:Stability-AI
# Stars:36612
# Resitory:https://github.com/Stability-AI/stablediffusion
# Created:2022-11-23T23:59:50Z
# Updated:2024-05-20T12:34:55Z
# Description:High-Resolution Image Synthesis with Latent Diffusion Models
#
# Name:interview_internal_reference
# Owner:0voice
# Stars:36173
# Resitory:https://github.com/0voice/interview_internal_reference
# Created:2019-06-10T06:54:19Z
# Updated:2024-05-20T12:04:15Z
# Description:2023年最新总结,阿里,腾讯,百度,美团,头条等技术面试题目,以及答案,专家出题人分析汇总。
#
# Name:odoo
# Owner:odoo
# Stars:35106
# Resitory:https://github.com/odoo/odoo
# Created:2014-05-13T15:38:58Z
# Updated:2024-05-20T10:30:35Z
# Description:Odoo. Open Source Apps To Grow Your Business.
#
# Name:mitmproxy
# Owner:mitmproxy
# Stars:34607
# Resitory:https://github.com/mitmproxy/mitmproxy
# Created:2010-02-16T04:10:13Z
# Updated:2024-05-20T11:21:02Z
# Description:An interactive TLS-capable intercepting HTTP proxy for penetration testers and software developers.
17.1.7 监视API的速率限制
浏览器地址栏输入:https://api.github.com/rate_limit
{"resources": {"core": {"limit": 60,"remaining": 59,"reset": 1716212802,"used": 1,"resource": "core"},"graphql": {"limit": 0,"remaining": 0,"reset": 1716214547,"used": 0,"resource": "graphql"},"integration_manifest": {"limit": 5000,"remaining": 5000,"reset": 1716214547,"used": 0,"resource": "integration_manifest"},"search": {"limit": 10,"remaining": 10,"reset": 1716211007,"used": 0,"resource": "search"}},"rate": {"limit": 60,"remaining": 59,"reset": 1716212802,"used": 1,"resource": "core"}
}