python - 使用 Python 从 Github 获取不一致的结果
问题描述
请参阅我的(python 代码新手)的想法是为每种编程语言获取星星并绘制它们。
简单的 python - 只需遍历 API 的结果并对结果进行排序,然后将顶部结果作为最高值。
我已经对此进行了测试,问题是我不断得到最终“java”的不一致结果
在代码块之后查看结果
import requests
from plotly import offline
# get all the urls
urls = {'python': 'https://api.github.com/search/repositories?q=language:python&sort=stars',
'c': 'https://api.github.com/search/repositories?q=language:c&sort=stars',
'ruby': 'https://api.github.com/search/repositories?q=language:ruby&sort=stars',
'java': 'https://api.github.com/search/repositories?q=language:java&sort=stars'}
# space for all the stars
python_stars = []
c_stars = []
ruby_stars = []
java_stars = []
headers = {'Accept': 'application/vnd.github.v3+json'}
# open the urls and populate the respective lists
for url in urls.values():
r = requests.get(url, headers=headers)
print(f"Status Code: {r.status_code}")
response_dict = r.json()
repo_dicts = response_dict['items']
for repo_dict in repo_dicts:
if repo_dict.get('language') == 'Python':
python_stars.append(repo_dict['stargazers_count'])
elif repo_dict['language'] == 'C':
c_stars.append(repo_dict['stargazers_count'])
elif repo_dict['language'] == 'Ruby':
ruby_stars.append(repo_dict['stargazers_count'])
elif repo_dict['language'] == 'Java':
java_stars.append(repo_dict['stargazers_count'])
# sort for the top stars
python_stars.sort(reverse=True)
c_stars.sort(reverse=True)
ruby_stars.sort(reverse=True)
java_stars.sort(reverse=True)
# get the top scores
top_python = python_stars[0]
top_c = c_stars[0]
top_ruby = ruby_stars[0]
top_java = java_stars[0]
langs = ['python', 'c', 'ruby', 'java']
top_stars = [top_python, top_c, top_ruby, top_java]
data = [{
'type': 'bar',
'x': langs,
'y': top_stars,
'marker': {
'color': 'rgb(255,100,150)',
'line': {'width': 1.5, 'color': 'rgb(25,25,25)'}
},
'opacity': 0.9
}]
my_layout = {
'title': 'Most Starred Projects on GitHub for Python, C, Ruby, Java',
'titlefont': {'size': 28},
'xaxis': {
'title': 'Repo',
'titlefont': {'size': 24},
'tickfont': {'size': 14},
},
'yaxis': {
'title': 'Stars',
'titlefont': {'size': 24},
'tickfont': {'size': 14},
}
}
fig = {'data': data, 'layout': my_layout}
offline.plot(fig, filename='toplangstars.html')`
我得到的结果是
运行 1 P = 109 C = 99 R = 46 J = 90
运行 2 P = 109 C = 99 R = 46 J = 43
运行 3 P = 109 C = 99 R = 46 J = 112
运行 4 P = 109 C = 99 R = 46 J = 112
运行 5 P = 109 C = 99 R = 46 J = 90
解决方案
您是否尝试通过用户和令牌来验证 github?
headers = {'Accept': 'application/vnd.github.v3+json'}
username = 'your user name'
token = 'your token'
# open the urls and populate the respective lists
for url in urls.values():
r = requests.get(url, headers=headers,auth=(username,token))
只更新这部分代码。如果您没有用户和令牌,您可以通过创建 github 应用程序从此处获取它。
有了这个,我尝试了几次并得到了相同的结果。
推荐阅读
- javascript - 重新导出 ES6 模块给出奇怪的行为
- kubernetes - 使用 nginx 反向代理在 Kubernetes 中公开服务
- vba - Vlookup 返回空白而不是 0 或 #N/A
- python - 上传多个文件时将 request.POST 与请求本身传递给 django 表单
- javascript - Promise.resolve() 只返回嵌套数组中的一个元素
- javascript - 在 5 秒内从 0 递增到一个值
- javascript - 是否可以执行一组步骤,包括在节点 js 中重新启动系统?
- spring-mvc - 在 Thymeleaf 中选中的复选框属性
- javascript - 带三路输出的短路评估
- typescript - 过滤对象的键,以便只返回指向数组的键(而不是'keyof'的所有键)