How to get JSON from webpage into Python script

How to get JSON from webpage into Python script

image_pdfimage_print

Python 2

import urllib, json
url = "http://maps.googleapis.com/maps/api/geocode/json?address=google"
response = urllib.urlopen(url)
data = json.loads(response.read())
print data

import json
import sys
import requests
callSign='EWG7KM'
adshex = '3c56e9'

url = "https://planefinder.net/api/api.php?r=aircraftMetadata&adshex={adsHex}&callsign={callsign}"
full_url = url.format(adsHex=adshex,callsign=callSign)
# Request data from planfinder.net 
data = json.loads(requests.get(full_url, headers={ # 'User-agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'}).content)

Python 3

import urllib.request
import json
hdr = {'User-agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'}
url = "https://opensky-network.org/api/flights/arrival?airport={icao}&begin={start}&end={end}" \ .format(icao=icao,start=start,end=end)
req = urllib.request.Request(url, headers=hdr) 
response = urllib.request.urlopen(req) 
data = response.read().decode('utf-8') 
print(data) 
r= json.loads(data)

Stackoverflow


Die Kommentare sind geschloßen.