快速对比多个库的数值

  • 学习来源:麻瓜编程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 上选出符合这些条件的项目:

  1. 最近一周内发布的
  2. 语言是 Python
  3. 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")
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值