w3resource

Python: Send a request to a web page, and print the JSON value of the response

Python Requests: Exercise-5 with Solution

Write a Python program to send a request to a web page, and print the JSON value of the response. Print each key value in the response.

Sample Solution:

Python Code:

import requests
r = requests.get('https://api.github.com/')
response = r.json()
print("JSON value of the said response:")
print(r.json())
print("\nEach key of the response:")
print("Current user url:",response['current_user_url'])
print("Current user authorizations html url:",response['current_user_authorizations_html_url'])
print("Authorizations url:",response['authorizations_url'])
print("code_search_url:",response['code_search_url'])
print("commit_search_url:",response['commit_search_url'])
print("Emails url:",response['emails_url'])
print("Emojis url:",response['emojis_url'])
print("Events url:",response['events_url'])
print("Feeds url:",response['feeds_url'])
print("Followers url:",response['followers_url'])
print("Following url:",response['following_url'])
print("Gists url:",response['gists_url'])
print("Issue search url:",response['issue_search_url'])
print("Issues url:",response['issues_url'])
print("Keys url:",response['keys_url'])
print("label search url:",response['label_search_url'])
print("Notifications url:",response['notifications_url'])
print("Organization url:",response['organization_url'])
print("Organization repositories url:",response['organization_repositories_url'])
print("Organization teams url:",response['organization_teams_url'])
print("Public gists url:",response['public_gists_url'])
print("Rate limit url:",response['rate_limit_url'])
print("Repository url:",response['repository_url'])
print("Repository search url:",response['repository_search_url'])
print("Current user repositories url:",response['current_user_repositories_url'])
print("Starred url:",response['starred_url'])
print("Starred gists url:",response['starred_gists_url'])
print("User url:",response['user_url'])
print("User organizations url:",response['user_organizations_url'])
print("User repositories url:",response['user_repositories_url'])
print("User search url:",response['user_search_url'])

Sample Output:

JSON value of the said response:
{'current_user_url': 'https://api.github.com/user', 'current_user_authorizations_html_url': 'https://github.com/settings/connections/applications{/client_id}', 'authorizations_url': 'https://api.github.com/authorizations', 'code_search_url': 'https://api.github.com/search/code?q={query}{&page,per_page,sort,order}', 'commit_search_url': 'https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}', 'emails_url': 'https://api.github.com/user/emails', 'emojis_url': 'https://api.github.com/emojis', 'events_url': 'https://api.github.com/events', 'feeds_url': 'https://api.github.com/feeds', 'followers_url': 'https://api.github.com/user/followers', 'following_url': 'https://api.github.com/user/following{/target}', 'gists_url': 'https://api.github.com/gists{/gist_id}', 'hub_url': 'https://api.github.com/hub', 'issue_search_url': 'https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}', 'issues_url': 'https://api.github.com/issues', 'keys_url': 'https://api.github.com/user/keys', 'label_search_url': 'https://api.github.com/search/labels?q={query}&repository_id={repository_id}{&page,per_page}', 'notifications_url': 'https://api.github.com/notifications', 'organization_url': 'https://api.github.com/orgs/{org}', 'organization_repositories_url': 'https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}', 'organization_teams_url': 'https://api.github.com/orgs/{org}/teams', 'public_gists_url': 'https://api.github.com/gists/public', 'rate_limit_url': 'https://api.github.com/rate_limit', 'repository_url': 'https://api.github.com/repos/{owner}/{repo}', 'repository_search_url': 'https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}', 'current_user_repositories_url': 'https://api.github.com/user/repos{?type,page,per_page,sort}', 'starred_url': 'https://api.github.com/user/starred{/owner}{/repo}', 'starred_gists_url': 'https://api.github.com/gists/starred', 'user_url': 'https://api.github.com/users/{user}', 'user_organizations_url': 'https://api.github.com/user/orgs', 'user_repositories_url': 'https://api.github.com/users/{user}/repos{?type,page,per_page,sort}', 'user_search_url': 'https://api.github.com/search/users?q={query}{&page,per_page,sort,order}'}

Each key of the response:
Current user url: https://api.github.com/user
Current user authorizations html url: https://github.com/settings/connections/applications{/client_id}
Authorizations url: https://api.github.com/authorizations
code_search_url: https://api.github.com/search/code?q={query}{&page,per_page,sort,order}
commit_search_url: https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}
Emails url: https://api.github.com/user/emails
Emojis url: https://api.github.com/emojis
Events url: https://api.github.com/events
Feeds url: https://api.github.com/feeds
Followers url: https://api.github.com/user/followers
Following url: https://api.github.com/user/following{/target}
Gists url: https://api.github.com/gists{/gist_id}
Issue search url: https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}
Issues url: https://api.github.com/issues
Keys url: https://api.github.com/user/keys
label search url: https://api.github.com/search/labels?q={query}&repository_id={repository_id}{&page,per_page}
Notifications url: https://api.github.com/notifications
Organization url: https://api.github.com/orgs/{org}
Organization repositories url: https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}
Organization teams url: https://api.github.com/orgs/{org}/teams
Public gists url: https://api.github.com/gists/public
Rate limit url: https://api.github.com/rate_limit
Repository url: https://api.github.com/repos/{owner}/{repo}
Repository search url: https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}
Current user repositories url: https://api.github.com/user/repos{?type,page,per_page,sort}
Starred url: https://api.github.com/user/starred{/owner}{/repo}
Starred gists url: https://api.github.com/gists/starred
User url: https://api.github.com/users/{user}
User organizations url: https://api.github.com/user/orgs
User repositories url: https://api.github.com/users/{user}/repos{?type,page,per_page,sort}
User search url: https://api.github.com/search/users?q={query}{&page,per_page,sort,order}

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python code to send a request to a web page, and print the information of headers. Also parse these values and print key-value pairs holding various information.
Next: Write a Python code to send a request to a web page and stop waiting for a response after a given number of seconds. In the event of times out of request, raise Timeout exception.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.