使用github开放的api读取我的用户信息
首先获取响应
import requests
response = requests.get("https://api.github.com/users/hooooolyshit") # ①
response
<Response [200]>
好了,响应成功,我们看看github给我们响应了什么。注意:响应格式一般为json格式,需要使用json方式解析
response.json()
{'login': 'hooooolyshit',
'id': 73675472,
'node_id': 'MDQ6VXNlcjczNjc1NDcy',
'avatar_url': 'https://avatars.githubusercontent.com/u/73675472?v=4',
'gravatar_id': '',
'url': 'https://api.github.com/users/hooooolyshit',
'html_url': 'https://github.com/hooooolyshit',
'followers_url': 'https://api.github.com/users/hooooolyshit/followers',
'following_url': 'https://api.github.com/users/hooooolyshit/following{/other_user}',
'gists_url': 'https://api.github.com/users/hooooolyshit/gists{/gist_id}',
'starred_url': 'https://api.github.com/users/hooooolyshit/starred{/owner}{/repo}',
'subscriptions_url': 'https://api.github.com/users/hooooolyshit/subscriptions',
'organizations_url': 'https://api.github.com/users/hooooolyshit/orgs',
'repos_url': 'https://api.github.com/users/hooooolyshit/repos',
'events_url': 'https://api.github.com/users/hooooolyshit/events{/privacy}',
'received_events_url': 'https://api.github.com/users/hooooolyshit/received_events',
'type': 'User',
'site_admin': False,
'name': 'Echo',
'company': None,
'blog': '',
'location': None,
'email': None,
'hireable': None,
'bio': None,
'twitter_username': None,
'public_repos': 7,
'public_gists': 0,
'followers': 1,
'following': 7,
'created_at': '2020-10-30T00:33:38Z',
'updated_at': '2022-06-26T15:32:28Z'}
使用pandas库分析一下吧
import pandas as pd
data = response.json() # ②
login = data['login'] # ③
name = data['name']
blog = data['blog']
public_repos = data['public_repos']
followers = data['followers']
html_url = data['html_url']
df = pd.DataFrame([[login, name, blog, public_repos, followers, html_url]],
columns=['login', 'name', 'blog', 'public_repos', 'followers', 'html_url']) # ④
df
login | name | blog | public_repos | followers | html_url | |
---|---|---|---|---|---|---|
0 | hooooolyshit | Echo | 7 | 1 | https://github.com/hooooolyshit |