- 学习来源:麻瓜编程https://www.mugglecode.com/
如何快速对比多个库的数值?
import requests
# https://api.github.com/search/repositories?q=django
# https://api.github.com/search/repositories?q=topic:django
def get_names():
names = input('请输入要查询的api名称,以空格分隔:')
return names.split()
def check_repos(names):
repo_api = 'https://api.github.com/search/repositories?q='
ecosys_api = 'https://api.github.com/search/repositories?q=topic:'
for name in names:
repo_info = requests.get(repo_api + name).json()['items'][0]
stars = repo_info['stargazers_count']
forks = repo_info['forks_count']
total_count = requests.get(ecosys_api+name).json()['total_count']
print(name)
print("stars:" + str(stars))
print("forks:" + str(forks))
print('Ecosys:' + str(total_count))
print('-'*20)
if __name__ == "__main__":
# names = ['django','flask','unittest']
names = get_names()
check_repos(names)
学会通过网站提供的API获取需要的数据
练习:找点 Python 小项目
从 GitHub 上选出符合这些条件的项目:
- 最近一周内发布的
- 语言是 Python
- size 小于200k的代码
把这些项目的链接 print 出来。
# coding:utf-8
import requests
"""
api形式 /code?q=language:python+size:<200+repo:目录名
q参数:
language:指定语言
size:指定文件大小,如size:<200表示文件小于200KB
repo:指定目录(必要参数)
示例:
https://api.github.com/search/code?q=language:python+size:<200+repo:tensorflow/tensorflow
"""
get_code_api = "https://api.github.com/search/code?q="
get_repo_api = "https://api.github.com/search/repositories?q=language:python"
# 编写函数,实现在github某一目录下寻找code文件的功能
def get_code(language, size, repo):
url = get_code_api + "language:" + language + "+size:" + size + "+repo:" + repo
# 访问GitHub接口
info = requests.get(url).json()
if 'items' in info:
for i in info['items']:
print(i['html_url'])
# 编写函数,查找更新时间在last_week之后的项目
def get_project(last_week):
# 访问GitHub接口
info = requests.get(get_repo_api).json()
for i in info['items']:
created_time = i['created_at']
if created_time > last_week:
language = "python"
size = "<200"
# 从info数据中获取项目的目录
repo = i['html_url'].replace("https://github.com/", "")
# 传入三个限制条件,调用查找code文件的函数
get_code(language, size, repo)
# 调用查找项目的函数,设定上个星期的时间
get_project("2019-05-19T00:00:00Z")