Home

Get A Web Page And Pull It Into A Variable In Python

Pull JSON from a url into a python variable with [TODO: Code shorthand span ]

python
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 start

Pull the page directly as text

python
#!/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)
~ fin ~