美文网首页
抓取GitHub网页出现的问题2

抓取GitHub网页出现的问题2

作者: Don_Mills_3675 | 来源:发表于2017-11-02 10:59 被阅读0次
1509591499(1).png

后来证实是输入的数据有问题,需要用str()转化一下,修改后的代码如下:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'处理API响应'
__author__ = 'Luna'

import requests
import sys  
import io  
import pygal
from pygal.style import LightenStyle as LS,LightColorizedStyle as LCS
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030') 
  

url='https://api.github.com/search/repositories'
url+='?q=language:c&sort=stars'
r=requests.get(url)
print('响应结果: ',r.status_code)

reaponses_dict=r.json()
print(reaponses_dict.keys())

print('total count: ',str(reaponses_dict['total_count']))

repo_dicts=reaponses_dict['items']
print('\n仓库返回',len(repo_dicts))

names=[]
plot_dicts=[]
print('每个仓库的相关信息')
for repo_dict in repo_dicts:
    names.append(repo_dict['name'])
    plot_dict={
        'value':repo_dict['stargazers_count'],
#此处‘label’的内容修改
        'label':str(repo_dict['description']),
        'xlink':repo_dict['html_url'],
        }   
    plot_dicts.append(plot_dict)
print(plot_dicts)   

python_style=LS('#336699',base_style=LCS)
chart=pygal.Bar(style=python_style,x_label_rotation=45,show_legend=False)
chart.title='最受欢迎的python项目'
chart.x_labels=names

file_path='C:\\Users\\luna\\Desktop\\documents\\data_science\\data_science\\python\\Python_first\\project_data\\下载数据\\使用API\\python_link.svg'
chart.add('',plot_dicts)
chart.render_to_file(file_path)

相关文章

网友评论

      本文标题:抓取GitHub网页出现的问题2

      本文链接:https://www.haomeiwen.com/subject/eoropxtx.html