python爬虫+可视化GitHub上标星最高的python项目
大三,刚开始学习python,把学习过程都记录下来,今天第一次写博客,利用一点时间做了这个项目,话不多说直接上图上代码。
import requests
import pygal
from pygal.style import LightColorizedStyle as lcs,LightenStyle as ls
#执行API调用并存储响应,language:python为选择python语言
url='https://api.github.com/search/repositories?q=language:python&sort=stars'
r=requests.get(url)
#打印200表示请求成功
print(r.status_code)
#将API响应存储在一个变量中
response_dict=r.json()
#处理结果
print('''total repositories(存储库总数):''',response_dict['total_count'])
#将仓库的信息存储到一个字典里
repo_dicts=response_dict['items']
#创建两个列表来存放x轴与y轴数据
names,stars=[],[]
for i in repo_dicts:
names.append(i['name'])
stars.append(i['stargazers_count'])
#可视化
my_style=ls('#333366',base_style=lcs)
chart=pygal.Bar(style=my_style,x_label_rotation=45,show_legend=False)
chart.title='GitHub最高星级的python项目'
chart.x_labels=names
chart.add('',stars)
chart.render_to_file('python_repos.svg')
总结
- 先用requests获取数据
- 分析出有用的数据
- 用pygal可视化数据,保存为.svg格式