Get A Web Page And Pull It Into A Variable In Python
Pull JSON from a url into a python variable with `requests``
Code
import requests
import json
url = 'https://raw.githubusercontent.com/alanwsmith/retrograde-data/main/current/mercury.json'
url = 'https://api.github.com/repos/alanwsmith/neopolitan'
url = 'https://api.github.com/users/alanwsmith'
request = requests.get(url)
json_contents = request.json()
output = json.dumps(
json_contents,
sort_keys=True,
indent=2,
default=str
)
print(output)
Results
{ "avatar_url": "https://avatars.githubusercontent.com/u/845049?v=4", "bio": "I like making things and taking notes. In fact, I spend a lot of time making things _to_ take notes", "blog": "https://www.alanwsmith.com/", "company": "alanwsmith.com", "created_at": "2011-06-12T11:08:56Z", "email": null, "events_url": "https://api.github.com/users/alanwsmith/events{/privacy}", "followers": 41, "followers_url": "https://api.github.com/users/alanwsmith/followers", "following": 45, "following_url": "https://api.github.com/users/alanwsmith/following{/other_user}", "gists_url": "https://api.github.com/users/alanwsmith/gists{/gist_id}", "gravatar_id": "", "hireable": null, "html_url": "https://github.com/alanwsmith", "id": 845049, "location": "the u.s.", "login": "alanwsmith", "name": "Alan Smith", "node_id": "MDQ6VXNlcjg0NTA0OQ==", "organizations_url": "https://api.github.com/users/alanwsmith/orgs", "public_gists": 16, "public_repos": 304, "received_events_url": "https://api.github.com/users/alanwsmith/received_events", "repos_url": "https://api.github.com/users/alanwsmith/repos", "site_admin": false, "starred_url": "https://api.github.com/users/alanwsmith/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/alanwsmith/subscriptions", "twitter_username": null, "type": "User", "updated_at": "2023-10-22T18:25:07Z", "url": "https://api.github.com/users/alanwsmith" }
Pull the page directly as text
Code
#!/usr/bin/env python
import requests
url = 'https://raw.githubusercontent.com/alanwsmith/retrograde-data/main/current/mercury.json'
request = requests.get('https://api.weather.gov/stations/KSGJ/observations')
contents = request.text
print(contents)