提交 2d0674a5 编写于 作者: 逆流者blog's avatar 逆流者blog 🇨🇳

Python3 使用API

上级 5c810891
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
# 自定义工具提示
# '#333366' 深蓝色
my_style = LS('#333366', base_style=LCS)
chart = pygal.Bar(style=my_style, x_label_rotation=45, show_legend=False)
chart.title = 'Python Projects'
chart.x_labels = ['httpie', 'django', 'flask']
plot_dicts = [
{'value': 16101, 'label': 'Description of httpie'},
{'value': 15028, 'label': 'Description of django'},
{'value': 14798, 'label': 'Description of flask'}
]
chart.add('', plot_dicts)
chart.render_to_file('bar_descriptions.svg')
<?xml version='1.0' encoding='utf-8'?>
<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" id="chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4" class="pygal-chart" viewBox="0 0 800 600"><!--Generated with pygal 2.4.0 (etree) ©Kozea 2012-2016 on 2020-10-28--><!--http://pygal.org--><!--http://github.com/Kozea/pygal--><defs><style type="text/css">#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4{-webkit-user-select:none;-webkit-font-smoothing:antialiased;font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .title{font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:16px}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .legends .legend text{font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:14px}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis text{font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:10px}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis text.major{font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:10px}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .text-overlay text.value{font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:16px}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .text-overlay text.label{font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:10px}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .tooltip{font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:14px}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 text.no_data{font-family:Consolas,"Liberation Mono",Menlo,Courier,monospace;font-size:64px}
#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4{background-color:#f8f8f8}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 path,#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 line,#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 rect,#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 circle{-webkit-transition:250ms ease-in;-moz-transition:250ms ease-in;transition:250ms ease-in}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .graph &gt; .background{fill:#f8f8f8}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .plot &gt; .background{fill:#ffffff}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .graph{fill:#333}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 text.no_data{fill:#666}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .title{fill:#666}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .legends .legend text{fill:#333}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .legends .legend:hover text{fill:#666}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis .line{stroke:#666}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis .guide.line{stroke:rgba(0,0,0,0.5)}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis .major.line{stroke:#333}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis text.major{fill:#666}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis.y .guides:hover .guide.line,#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .line-graph .axis.x .guides:hover .guide.line,#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .stackedline-graph .axis.x .guides:hover .guide.line,#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .xy-graph .axis.x .guides:hover .guide.line{stroke:#666}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis .guides:hover text{fill:#666}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .reactive{fill-opacity:.5;stroke-opacity:.8}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .ci{stroke:#333}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .reactive.active,#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .active .reactive{fill-opacity:.9;stroke-opacity:.9;stroke-width:4}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .ci .reactive.active{stroke-width:1.5}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .series text{fill:#666}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .tooltip rect{fill:#ffffff;stroke:#666;-webkit-transition:opacity 250ms ease-in;-moz-transition:opacity 250ms ease-in;transition:opacity 250ms ease-in}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .tooltip .label{fill:#333}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .tooltip .label{fill:#333}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .tooltip .legend{font-size:.8em;fill:rgba(0,0,0,0.5)}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .tooltip .x_label{font-size:.6em;fill:#666}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .tooltip .xlink{font-size:.5em;text-decoration:underline}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .tooltip .value{font-size:1.5em}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .bound{font-size:.5em}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .max-value{font-size:.75em;fill:rgba(0,0,0,0.5)}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .map-element{fill:#ffffff;stroke:rgba(0,0,0,0.5) !important}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .map-element .reactive{fill-opacity:inherit;stroke-opacity:inherit}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .color-0,#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .color-0 a:visited{stroke:#333366;fill:#333366}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .text-overlay .color-0 text{fill:black}
#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 text.no_data{text-anchor:middle}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .guide.line{fill:none}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .centered{text-anchor:middle}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .title{text-anchor:middle}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .legends .legend text{fill-opacity:1}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis.x text{text-anchor:middle}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis.x:not(.web) text[transform]{text-anchor:start}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis.x:not(.web) text[transform].backwards{text-anchor:end}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis.y text{text-anchor:end}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis.y text[transform].backwards{text-anchor:start}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis.y2 text{text-anchor:start}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis.y2 text[transform].backwards{text-anchor:end}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis .guide.line{stroke-dasharray:4,4}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis .major.guide.line{stroke-dasharray:6,6}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .horizontal .axis.y .guide.line,#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .horizontal .axis.y2 .guide.line,#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .vertical .axis.x .guide.line{opacity:0}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .horizontal .axis.always_show .guide.line,#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .vertical .axis.always_show .guide.line{opacity:1 !important}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis.y .guides:hover .guide.line,#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis.y2 .guides:hover .guide.line,#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis.x .guides:hover .guide.line{opacity:1}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .axis .guides:hover text{opacity:1}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .nofill{fill:none}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .subtle-fill{fill-opacity:.2}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .dot{stroke-width:1px;fill-opacity:1}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .dot.active{stroke-width:5px}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .dot.negative{fill:transparent}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 text,#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 tspan{stroke:none !important}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .series text.active{opacity:1}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .tooltip rect{fill-opacity:.95;stroke-width:.5}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .tooltip text{fill-opacity:1}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .showable{visibility:hidden}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .showable.shown{visibility:visible}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .gauge-background{fill:rgba(229,229,229,1);stroke:none}#chart-6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4 .bg-lines{stroke:#f8f8f8;stroke-width:2px}</style><script type="text/javascript">window.pygal = window.pygal || {};window.pygal.config = window.pygal.config || {};window.pygal.config['6a0ec9c0-9a67-4cf8-b3f5-079ebd5184c4'] = {"allow_interruptions": false, "box_mode": "extremes", "classes": ["pygal-chart"], "css": ["file://style.css", "file://graph.css"], "defs": [], "disable_xml_declaration": false, "dots_size": 2.5, "dynamic_print_values": false, "explicit_size": false, "fill": false, "force_uri_protocol": "https", "formatter": null, "half_pie": false, "height": 600, "include_x_axis": false, "inner_radius": 0, "interpolate": null, "interpolation_parameters": {}, "interpolation_precision": 250, "inverse_y_axis": false, "js": ["//kozea.github.io/pygal.js/2.0.x/pygal-tooltips.min.js"], "legend_at_bottom": false, "legend_at_bottom_columns": null, "legend_box_size": 12, "logarithmic": false, "margin": 20, "margin_bottom": null, "margin_left": null, "margin_right": null, "margin_top": null, "max_scale": 16, "min_scale": 4, "missing_value_fill_truncation": "x", "no_data_text": "No data", "no_prefix": false, "order_min": null, "pretty_print": false, "print_labels": false, "print_values": false, "print_values_position": "center", "print_zeroes": true, "range": null, "rounded_bars": null, "secondary_range": null, "show_dots": true, "show_legend": false, "show_minor_x_labels": true, "show_minor_y_labels": true, "show_only_major_dots": false, "show_x_guides": false, "show_x_labels": true, "show_y_guides": true, "show_y_labels": true, "spacing": 10, "stack_from_top": false, "strict": false, "stroke": true, "stroke_style": null, "style": {"background": "#f8f8f8", "ci_colors": [], "colors": ["#333366", "#3c3c79", "#46468c", "#4f4f9f", "#5d5dae", "#6f6fb7", "#8282c1", "#9595ca", "#a8a8d4", "#bbbbdd"], "font_family": "Consolas, \"Liberation Mono\", Menlo, Courier, monospace", "foreground": "#333", "foreground_strong": "#666", "foreground_subtle": "rgba(0, 0 , 0, 0.5)", "guide_stroke_dasharray": "4,4", "label_font_family": "Consolas, \"Liberation Mono\", Menlo, Courier, monospace", "label_font_size": 10, "legend_font_family": "Consolas, \"Liberation Mono\", Menlo, Courier, monospace", "legend_font_size": 14, "major_guide_stroke_dasharray": "6,6", "major_label_font_family": "Consolas, \"Liberation Mono\", Menlo, Courier, monospace", "major_label_font_size": 10, "no_data_font_family": "Consolas, \"Liberation Mono\", Menlo, Courier, monospace", "no_data_font_size": 64, "opacity": ".5", "opacity_hover": ".9", "plot_background": "#ffffff", "stroke_opacity": ".8", "stroke_opacity_hover": ".9", "title_font_family": "Consolas, \"Liberation Mono\", Menlo, Courier, monospace", "title_font_size": 16, "tooltip_font_family": "Consolas, \"Liberation Mono\", Menlo, Courier, monospace", "tooltip_font_size": 14, "transition": "250ms ease-in", "value_background": "rgba(229, 229, 229, 1)", "value_colors": [], "value_font_family": "Consolas, \"Liberation Mono\", Menlo, Courier, monospace", "value_font_size": 16, "value_label_font_family": "Consolas, \"Liberation Mono\", Menlo, Courier, monospace", "value_label_font_size": 10}, "title": "Python Projects", "tooltip_border_radius": 0, "tooltip_fancy_mode": true, "truncate_label": null, "truncate_legend": null, "width": 800, "x_label_rotation": 45, "x_labels": ["httpie", "django", "flask"], "x_labels_major": null, "x_labels_major_count": null, "x_labels_major_every": null, "x_title": null, "xrange": null, "y_label_rotation": 0, "y_labels": null, "y_labels_major": null, "y_labels_major_count": null, "y_labels_major_every": null, "y_title": null, "zero": 0, "legends": [""]}</script><script type="text/javascript" xlink:href="https://kozea.github.io/pygal.js/2.0.x/pygal-tooltips.min.js" /></defs><title>Python Projects</title><g class="graph bar-graph vertical"><rect x="0" y="0" width="800" height="600" class="background" /><g transform="translate(60, 46)" class="plot"><rect x="0" y="0" width="714.5441558772843" height="498.5441558772843" class="background" /><g class="axis y always_show"><g class="guides"><path d="M0.000000 488.956768 h714.544156" class="axis major line" /><text x="-5" y="492.4567682642596" class="major">0</text><title>0</title></g><g class="guides"><path d="M0.000000 429.411475 h714.544156" class="guide line" /><text x="-5" y="432.91147534441177" class="">2000</text><title>2000</title></g><g class="guides"><path d="M0.000000 369.866182 h714.544156" class="guide line" /><text x="-5" y="373.36618242456393" class="">4000</text><title>4000</title></g><g class="guides"><path d="M0.000000 310.320890 h714.544156" class="guide line" /><text x="-5" y="313.82088950471615" class="">6000</text><title>6000</title></g><g class="guides"><path d="M0.000000 250.775597 h714.544156" class="guide line" /><text x="-5" y="254.2755965848683" class="">8000</text><title>8000</title></g><g class="guides"><path d="M0.000000 191.230304 h714.544156" class="major guide line" /><text x="-5" y="194.73030366502047" class="major">10000</text><title>10000</title></g><g class="guides"><path d="M0.000000 131.685011 h714.544156" class="guide line" /><text x="-5" y="135.1850107451727" class="">12000</text><title>12000</title></g><g class="guides"><path d="M0.000000 72.139718 h714.544156" class="guide line" /><text x="-5" y="75.63971782532485" class="">14000</text><title>14000</title></g><g class="guides"><path d="M0.000000 12.594425 h714.544156" class="guide line" /><text x="-5" y="16.09442490547707" class="">16000</text><title>16000</title></g></g><g class="axis x"><path d="M0.000000 0.000000 v498.544156" class="line" /><g class="guides"><path d="M128.251515 0.000000 v498.544156" class="guide line" /><text x="128.25151515746126" y="513.5441558772843" class="" transform="rotate(45 128.251515 513.544156)">httpie</text></g><g class="guides"><path d="M357.272078 0.000000 v498.544156" class="guide line" /><text x="357.27207793864216" y="513.5441558772843" class="" transform="rotate(45 357.272078 513.544156)">django</text></g><g class="guides"><path d="M586.292641 0.000000 v498.544156" class="guide line" /><text x="586.292640719823" y="513.5441558772843" class="" transform="rotate(45 586.292641 513.544156)">flask</text></g></g><g class="series serie-0 color-0"><g class="bars"><g class="bar"><desc class="label">Description of httpie</desc><rect x="39.57475324858805" y="9.587387613024703" rx="0" ry="0" width="177.35352381774646" height="479.3693806512349" class="rect reactive tooltip-trigger" /><desc class="value">16101</desc><desc class="x centered">128.25151515746128</desc><desc class="y centered">249.27207793864216</desc><desc class="x_label">httpie</desc></g><g class="bar"><desc class="label">Description of django</desc><rect x="268.5953160297689" y="41.53343726452306" rx="0" ry="0" width="177.35352381774646" height="447.42333099973655" class="rect reactive tooltip-trigger" /><desc class="value">15028</desc><desc class="x centered">357.27207793864216</desc><desc class="y centered">265.24510276439133</desc><desc class="x_label">django</desc></g><g class="bar"><rect x="497.61587881094977" y="48.38114595030561" rx="0" ry="0" width="177.35352381774646" height="440.575622313954" class="rect reactive tooltip-trigger" /><desc class="value">14798</desc><desc class="x centered">586.292640719823</desc><desc class="y centered">268.6689571072826</desc><desc class="x_label">flask</desc></g></g></g></g><g class="titles"><text x="400.0" y="26" class="title plot_title">Python Projects</text></g><g transform="translate(60, 46)" class="plot overlay"><g class="series serie-0 color-0" /></g><g transform="translate(60, 46)" class="plot text-overlay"><g class="series serie-0 color-0" /></g><g transform="translate(60, 46)" class="plot tooltip-overlay"><g transform="translate(0 0)" style="opacity: 0" class="tooltip"><rect rx="0" ry="0" width="0" height="0" class="tooltip-box" /><g class="text" /></g></g></g></svg>
\ No newline at end of file
此差异已折叠。
import requests
import pygal
import json
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
# 执行API调用并存储响应
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
# 因为访问github api有访问次数限制,访问成功我生成一个 github_python_repositories.json 文件,如果达到限制次数,就读取本地的文件
filename = 'github_python_repositories.json'
try:
r = requests.get(url)
print("Status code: ", r.status_code)
if r.status_code == 403:
raise Exception('达到访问限制次数,一分钟后再试!')
# 将API响应存储在一个变量中
response_dict = r.json()
except Exception as err:
print('访问github api 异常:', err)
with open(filename) as f:
response_dict = json.load(f)
else:
# 将数据写入文件
with open(filename, 'w') as f:
f.write(r.text)
print("Total Repositories: ", response_dict['total_count'])
# 搜索有关仓库的信息
# repo_dicts 列表
repo_dicts = response_dict['items']
print("Repositories returned: ", len(repo_dicts))
names, plot_dicts = [], []
for repo_dict in repo_dicts:
names.append(repo_dict['name'])
# description 可能为空
plot_dict = {
'value': repo_dict['stargazers_count'],
'label': str(repo_dict['description']),
'xlink': repo_dict['html_url'],
}
plot_dicts.append(plot_dict)
# 可视化
# '#333366' 深蓝色
my_style = LS('#333366', base_style=LCS)
my_config = pygal.Config()
# x_label_rotation=45 让标签让x轴旋转45度
my_config.x_label_rotation = 45
# show_legend=False 隐藏图例
my_config.show_legend = False
# 将较长的项目名缩短为15个字符
my_config.truncate_label = 15
# 隐藏图表中的水平线
my_config.show_y_guides = False
# 设置宽度
my_config.width = 1000
chart = pygal.Bar(my_config, style=my_style)
chart.title = 'GitHub上更多的Star的python工程'
chart.x_labels = names
chart.add('', plot_dicts)
chart.render_to_file('python_repos.svg')
# 研究第一个仓库
# repo_dict = repo_dicts[0]
# print("\nkeys: ", len(repo_dict))
# for key in sorted(repo_dict.keys()):
# print(key)
# print("\n显示第一个仓库的信息:")
# print('Name:', repo_dict['name'])
# print('Owner:', repo_dict['owner']['login'])
# print('Star:', repo_dict['stargazers_count'])
# print('Repository:', repo_dict['html_url'])
# print('Created:', repo_dict['created_at'])
# print('Updated:', repo_dict['updated_at'])
# print('Description:', repo_dict['description'])
# 处理结果
# print(response_dict.keys())
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册