美文网首页
AttributeError: 'NoneType

AttributeError: 'NoneType

作者: 赵者也 | 来源:发表于2018-02-06 18:24 被阅读84次

    解决方法一:

    # -*- coding: utf-8 -*-
    # Created by: ZhaoDongshuang
    # Created on: 18-2-6
    
    import requests
    import pygal
    
    from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
    
    # 执行 API 调用并存储响应
    url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
    r = requests.get(url)
    print("Status code:", r.status_code)
    # 将 API 响应存储在一个变量中
    response_dict = r.json()
    print("Total repositories:", response_dict['total_count'])
    # 探索有关仓库的信息
    repo_dicts = response_dict['items']
    
    print("Number of items:", len(repo_dicts))
    
    
    def stargazers_count(repo_dict_json):
        return repo_dict_json['stargazers_count']
    
    
    def description(repo_dict_json):
        description_str = repo_dict_json['description']
        if description_str:
            return description_str
        else:
            return ""
    
    
    names, plot_dicts = [], []
    for repo_dict in repo_dicts:
        names.append(repo_dict['name'])
        plot_dict = {
            'value': stargazers_count(repo_dict),
            'label': description(repo_dict),
        }
        plot_dicts.append(plot_dict)
    
    # 可视化
    my_style = LS('#333366', base_style=LCS)
    my_config = pygal.Config()                  # 用于定制图表的外观
    my_config.x_label_rotation = 45             # 标签绕 x 轴旋转 45 度
    my_config.show_legend = False               # 隐藏图例
    my_config.title_font_size = 24              # 设置图表标题的字体大小
    my_config.label_font_size = 14              # 设置图副标签的字体大小
    my_config.major_label_font_size = 18        # 设置主标签的字体大小
    my_config.truncate_label = 15               # 仅显示 15 个字符
    my_config.show_y_guides = False             # 隐藏图表中的水平线
    my_config.width = 1000                      # 设置自定义宽度
    
    chart = pygal.Bar(my_config, style=my_style)
    chart.title = 'Most-Starred Python Projects on GitHub'
    chart.x_labels = names
    
    chart.add('', plot_dicts)
    chart.render_to_file('python_repos.svg')
    
    

    解决方法二:

    'label': str(repo_dict['description'])
    

    相关文章

      网友评论

          本文标题:AttributeError: 'NoneType

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