使用Python从Github获取不一致的结果

使用Python从Github获取不一致的结果,python,github-api,Python,Github Api,请看我(python代码新手)的想法是为每种编程语言获取星星并绘制它们 简单python—只需迭代API中的结果并对结果进行排序,然后将顶部结果作为最高结果 我已经对此进行了测试,问题是我在最终的“java”中不断得到不一致的结果 请参见代码块后的结果 import requests from plotly import offline # get all the urls urls = {'python': 'https://api.github.com/search/repositorie

请看我(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应用程序从中获取


有了这个,我试了好几次,都得到了同样的结果。

我看不出你有什么缺点。您是否尝试输出原始json,并检查结果是否不同。如果是这样,那么您就知道这不是任何解析问题