Python 3.7之使用web api

发布时间:2019-07-29 10:28:41编辑:auto阅读(1444)

    1.调用GitHub的公共API

    #需要提前安装requests 包
    
    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(response_dict.keys())
    print("Total repositories:",response_dict['total_count'])
    
    #探索有关GitHub仓库的信息
    repo_dicts = response_dict['items']
    print("Repositories returned:",len(repo_dicts))
    
    #研究第一个GitHub仓库
    repo_dict = repo_dicts[0]
    print("\nkeys:",len(repo_dict))
    for key in sorted(repo_dict.keys()):
        print(key)
    
    print("\nSelected information about first repository:")
    print('Name:',repo_dict['name'])
    print('Owner:',repo_dict['owner']['login'])

     

关键字