访问地址:https://api.github.com/search/repositories?q=language:python&dort=stars
1.安装requests
1)检查pip版本:pip --version
Windows:
pip --version
pip 9.0.1 from E:\ProgramData\Anaconda3\lib\site-packages (python 3.6)
2)如果没有pip 可通过 python get-pip.py
3)升级pip python pip -m install --upgrade pip
4)pip install requests
执行:
import requests
url =‘https://api.github.com/search/repositories?q=language:python&dort=stars’
r = requests.get(url)
print(“status code”,r.status_code)
response_dic=r.json()
print(response_dic.keys())
result:
E:\ProgramData\Anaconda3\python.exe “C:/Users/zhangqingling/PycharmProjects/call API.py”
status code 200
dict_keys([‘total_count’, ‘incomplete_results’, ‘items’])
Process finished with exit code 0
处理响应字典:
response_dic=r.json()
print(response_dic.keys())
print(“total”,response_dic[‘total_count’])
repo = response_dic[‘items’]
print(“items count” , len(repo) )
repofirst = repo[0]
for key in sorted(repofirst.keys()):
print(key)
- sorted 获取已排序列表副本
- r.headers[‘content-type’]
- r.json()
API:get,post ,PUT, DELETE, HEAD and OPTIONS
python:
r = requests.get(‘https://api.github.com/events’)
HTTP POST request
r = requests.post(‘https://httpbin.org/post’, data = {‘key’:‘value’})
PUT, DELETE, HEAD and OPTIONS
r = requests.put(‘https://httpbin.org/put’, data = {‘key’:‘value’})
r = requests.delete(‘https://httpbin.org/delete’)
r = requests.head(‘https://httpbin.org/get’)
r = requests.options(‘https://httpbin.org/get’)
binary responsefrom PIL import Image
from io import BytesIO
i = Image.open(BytesIO(r.content))
**Json response:**r.Json
监视API 速率限制
https://api.github.com/rate_limit
可视化 pygal 可视化仓库,可以在执行路径下查看生成文件(‘python_repos.svg’)
- pip install pygal
import requests
#导入pygal可视化模块
import pygal
#Pygal样式保存在模块style中,包括LightenStyle 调整颜色和LightColorizedStyle加亮颜色
from pygal.style import LightColorizedStyle as LCS,LightenStyle as LS
url =‘https://api.github.com/search/repositories?q=language:python&dort=stars’
r = requests.get(url)
print(“status code”,r.status_code)
#读取response to Json
response_dic=r.json()
repos = response_dic[‘items’]
names,stars=[],[]
for repo_dict in repos:
names.append(repo_dict[‘name’])
stars.append(repo_dict[‘stargazers_count’])
my_style =LS(’#333366’,base_style=LCS)
#条形图设置
chart =pygal.Bar(style=my_style,x_label_rotation=45,show_legend=False)
chart.title =‘most-starred Python Project on GitHub’
#X轴
chart.x_labels =names
#添加数据
chart.add(’’,stars)
#保存在文件
chart.render_to_file(‘python_repos.svg’)
chart.render()
支持的各种图形:
http://www.pygal.org/en/stable/documentation/types/index.html
使用config配置:
my_config =pygal.Config()
my_config.x_label_rotation=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
my_config.show_y_guides = False
my_config .width =1000
#chart =pygal.Bar(style=my_style,x_label_rotation=45,show_legend=False)
chart =pygal.Bar(my_config,style=my_style)
3.鼠标指向图形显示它表示的信息
import requests
import pygal
from pygal.style import LightColorizedStyle as LCS,LightenStyle as LS
url =‘https://api.github.com/search/repositories?q=language:python&dort=stars’
r = requests.get(url)
print(“status code”,r.status_code)
response_dic=r.json()
repos = response_dic[‘items’]
names,plot_dicts = [],[]
my_style =LS(’#333366’,base_style=LCS)
chart =pygal.Bar(style=my_style,x_label_rotation=45,show_legend=False)
chart.title=‘Python Project’
for repo_dict in repos:
names.append(repo_dict[‘name’])
plot_dict ={
‘value’: repo_dict[‘stargazers_count’],
‘label’: repo_dict[‘description’],
}
plot_dicts.append(plot_dict)
print(plot_dicts)
chart.add(’’,plot_dicts)
chart.x_labels = names
chart.render_to_file(‘bar_Desc1.svg’)
get error as below:
File “E:\ProgramData\Anaconda3\lib\site-packages\pygal\util.py”, line 233, in decorate
metadata[‘label’])
File “E:\ProgramData\Anaconda3\lib\site-packages\pygal_compat.py”, line 61, in to_unicode
return string.decode(‘utf-8’)
AttributeError: ‘NoneType’ object has no attribute ‘decode’
解决方法:
plot_dict ={
‘value’: repo_dict[‘stargazers_count’],
‘label’: str(repo_dict[‘description’]),
}