美文网首页
《Python编程:从入门到实践》学习记录(17)项目-使用ap

《Python编程:从入门到实践》学习记录(17)项目-使用ap

作者: 垃圾简书_吃枣药丸 | 来源:发表于2020-08-20 09:15 被阅读0次
    import requests
    import pygal
    from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
    
    
    def download_data(url: str):
        """下载api数据"""
        response = requests.get(url, timeout=None)
        print("响应状态码为: " + str(response.status_code))
        resp_json = response.json()
        items = resp_json["items"]
        rep_names, rep_star_count, rep_fork_count, rep_open_issue_count = [], [], [], []
        for item in items:
            rep_names.append(item["name"])
            rep_star_count.append(item["stargazers_count"])
            rep_fork_count.append(item["forks_count"])
            rep_open_issue_count.append(item["open_issues_count"])
        return {"rep_names": rep_names, "rep_star_count": rep_star_count, "forks_count": rep_fork_count,
                "open_issues_count": rep_open_issue_count}
    
    
    def draw(json_date: dict):
        """绘图"""
        my_style = LS('#333366', base_style=LCS)
        # x轴文本旋转45度,展示图例
        chart = pygal.Bar(style=my_style, x_label_rotation=45, show_legend=True)
        chart.title = "Github上最受欢迎的python项目"
        chart.x_labels = json_date["rep_names"]
        chart.add("star数量", json_date["rep_star_count"])
        chart.add("fork数量", json_date["forks_count"])
        chart.add("open issue数量", json_date["open_issues_count"])
        chart.render_to_file('github-python.svg')
    
    
    url = "https://api.github.com/search/repositories?q=language:python&sort=stars"
    result = download_data(url)
    draw(result)
    
    • 结果


      image.png

    相关文章

      网友评论

          本文标题:《Python编程:从入门到实践》学习记录(17)项目-使用ap

          本文链接:https://www.haomeiwen.com/subject/dowrjktx.html