我想从github api中提取用户详细信息,但是当我尝试将结果转换为json文件时,却给了我错误:
URL = 'https://api.github.com/users/awais4302'
r = requests.get(URL,headers=headers)
print(r.json)
for aw in r.json():
print(aw['id'])
输出:
{'login': 'awais4302', 'id': 27438348, 'node_id': 'MDQ6VXNlcjI3NDM4MzQ4', `'avatar_url': 'https://avatars0.githubusercontent.com/u/27438348?v=4', 'gravatar_id': '', 'url': 'https://api.github.com/users/awais4302', 'html_url': 'https://github.com/awais4302', 'followers_url': 'https://api.github.com/users/awais4302/followers', 'following_url': 'https://api.github.com/users/awais4302/following{/other_user}', 'gists_url': 'https://api.github.com/users/awais4302/gists{/gist_id}', 'starred_url': 'https://api.github.com/users/awais4302/starred{/owner}{/repo}', 'subscriptions_url': 'https://api.github.com/users/awais4302/subscriptions', 'organizations_url': 'https://api.github.com/users/awais4302/orgs', 'repos_url': 'https://api.github.com/users/awais4302/repos', 'events_url': 'https://api.github.com/users/awais4302/events{/privacy}', 'received_events_url': 'https://api.github.com/users/awais4302/received_events', 'type': 'User', 'site_admin': False, 'name': None, 'company': None, 'blog': '', 'location': None, 'email': None, 'hireable': None, 'bio': None, 'public_repos': 1, 'public_gists': 0, 'followers': 0, 'following': 0, 'created_at': '2017-04-13T16:53:35Z', 'updated_at': '2019-07-03T08:36:45Z', 'private_gists': 0, 'total_private_repos': 0, 'owned_private_repos': 0, 'disk_usage': 0, 'collaborators': 0, 'two_factor_authentication': False, 'plan': {'name': 'free', 'space': 976562499, 'collaborators': 0, 'private_repos': 10000}}
---------------------------------------------------------------------------
`TypeError Traceback (most recent call last)
<ipython-input-14-6d0e261b00e5> in <module>
25 print(r.json())
26 for aw in r.json():
---> 27 print(aw['login'])
28
29
TypeError: string indices must be integers
我想要从给定链接中提取用户的姓名,位置等所有属性的输出。
答案 0 :(得分:0)
尝试如下。
URL = 'https://api.github.com/users/awais4302'
r = requests.get(URL,headers=headers)
for k, v in r.json().items():
print k, ":", v
访问特定键的值,例如id
data = r.json()
print data['id']
说明:
json
方法返回一个字典,而您试图仅遍历键。