如何在循环API时遇到错误

时间:2017-02-28 02:46:33

标签: python api github

非常简单的问题,我尝试了多种修复方法,但我认为我正在通过一些非常容易修复的方法。

import requests

# Make an API call and store the response.
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'
r = requests.get(url)
print("Status code:", r.status_code)

# Store API response in a variable.
response_dict = r.json()
print("Total respositories:", response_dict['total_count'])

# Explore information about the respositories.
repo_dicts = response_dict['items']
print("Respositories returned:", len(repo_dicts))

print("\nSelected information about each respository:")
for repo_dict in repo_dicts:

    print('\nName:', repo_dict['name'])
    print('Owner:', repo_dict['owner']['login'])
    print('Stars:', repo_dict['stargazers_count'])
    print('Respository:', repo_dict['html_url'])
    print('Description:', repo_dict['description'])

通过github api循环播放大多数已加星标的项目,并打印有关每个存储库的信息。其中一个respositorys'没有描述,所以我如何跳过该描述或说“没有描述可用'”,而没有我的程序崩溃。

感谢。

2 个答案:

答案 0 :(得分:1)

这可能适合你。只是一个简单的if语句来检查空字符串。

SUM/* comment */({custrecord_hm_bc_payroll_net_pay}) OVER(PARTITION BY {custrecord_advs_hold_unhold_status} ORDER BY {custrecord_hm_bc_payroll_emp_id} ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)

答案 1 :(得分:1)

这可能适合你:

for repo_dict in repo_dicts:

    print('\nName:', repo_dict.get('name', None))
    print('Owner:', repo_dict['owner'].get('login', None))
    print('Stars:', repo_dict.get('stargazers_count', None))
    print('Respository:', repo_dict.get('html_url', None))
    print('Description:', repo_dict.get('description', None))

如果键的值为空,则返回None