import requests import pygal # 执行API调用并存储响应 url = 'https://api.github.com/search/repositories?q=language:python&sort=stars' # 获取响应对象 r = requests.get(url) # status_code属性,是否请求成功,200代表请求成功 print("Status code:", r.status_code) # 将API响应返回一个json格式的数据,使用json()方法存储在一个字典中 response_dict = r.json() print("Total repositories:", response_dict['total_count']) repo_dicts = response_dict['items'] print("Repositories returned:", len(repo_dicts)) names,stars=[],[] for repo_dict in repo_dicts: names.append(repo_dict['name']) stars.append(repo_dict['stargazers_count']) #可视化 #创建pygal配置对象 my_config=pygal.Config() my_config.x_label_rotation=60 #设置x轴刻度的旋转度数 my_config.y_label_rotation=60 my_config.title_font_size=68 #设置标题字体 my_config.label_font_size=14 #设置刻度的字体 my_config.truncate_label=5 #设置缩进刻度字符个数 my_config.width=800 #设置宽度 #创建pygal可视化对象 win=pygal.Bar(my_config) win.title="title :AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" win.x_title="XXXXXXXXXXXXXXXXXXX" win.y_title="YYYYYYYYYYYYYYYYYYY" win.x_labels=names #x轴刻度数据 win.add("stars count",stars) #参一:数据的表述;参二:Y轴数据 win.add("my1 ",stars) win.add("my2 ",stars) win.add("my3 ",stars) win.render_to_file("requests.svg") #递交到文件