python 17-3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import sys
import requests
import unittest
 
url = 'https://api.github.com/search/repositories?q=language:java&sort=starts'
# requests.get(url) 获取连接的数据
r = requests.get(url)
#  验证 r.status_code 返回状态码
class un(unittest.TestCase):
    def test(self):
        x = r.status_code
        self.assertEquals = (x,'200')
unittest.main()
# 转换成python可以接受格式
response_dict = r.json()
"""with open("api.json","w") as f:
    for i in response_dict:
        f.write(str(i))"""
# 输出字典中的键值
print(response_dict.keys())
print("Total_Count", response_dict["total_count"])
# 输出总的项目书
repo_dicts = response_dict["items"]
print("Items", len(repo_dicts))
# 导入pygal以及pygal的样式
import pygal
from pygal.style import LightColorizedStyle as LCS, LightenStyle as LS
# 获取拥有者和星数
stars, names, plot_dicts = [], [], []
for repo_dict in repo_dicts:
    names.append(repo_dict['name'])
    # stars.append(repo_dict['stargazers_count'])
# 自定义提示工具
    plot_dict = {
        'value': repo_dict['stargazers_count'],
        'label': str(repo_dict['description']),
        'xlink':repo_dict['html_url'],
    }
    plot_dicts.append(plot_dict)
# 图像化
my_style = LS('#333366', base_style=LCS)  # 设置图形样式
# 自定义样式
my_config = pygal.Config()
my_config.x_label_rotation = 45
my_config.show_legend = False
my_config.title_font_size = 24
my_config.labele_font_size = 14
my_config.major_lable_font_size = 18
my_config.truncate_label = 15
my_config.show_y_guides = False
my_config.width = 1000
# x_label_rotation 旋转角度 show_legend 隐藏图例
# chart = pygal.Bar(style=my_style, x_lable_rotation=45, show_legend=False)
chart = pygal.Bar(my_config, style=my_style)
chart.title = 'Famous Java Github'
chart.x_labels = names
chart.x_title = 'name'
# chart.add('', stars)
# 方法二
chart.add('', plot_dicts)
chart.render_to_file('python_repo.svg')
# 获取第一个项目的数量
repo_dict = repo_dicts[0]
print("\nKeys", len(repo_dict))
# 输出关键数据
with open('list.txt','w') as d:
    d.write(repo_dict['name'])
    d.write('\n'+repo_dict['owner']['login'])
    d.write('\n'+repo_dict['html_url'])
    d.write('\n'+str(repo_dict['stargazers_count']))
    d.write('\n'+repo_dict['created_at'])
    d.write('\n'+repo_dict['updated_at'])
    d.write('\n'+repo_dict['description'])
    for i in repo_dict:
            d.write('\n'+i)
北关漫步

北关漫步

咸鱼....

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>