Python API Tutorial - Getting Started With APIs - Dataquest
Python API Tutorial - Getting Started With APIs - Dataquest
What is an API?
import requests
Email address
Now that we’ve installed and imported the
requests library, let’s start using it. Password
Start Now
Making Our First API Request
response = requests.get("https://api.open-notify.org/t
print(response.status_code)
404
API Documentation
response = requests.get("https://api.open-notify.org/a
print(response.status_code)
200
import json
def jprint(obj):
print(text)
jprint(response.json())
{
"message": "success",
"number": 6,
"people": [
"craft": "ISS",
},
"craft": "ISS",
},
"craft": "ISS",
},
"craft": "ISS",
},
"craft": "ISS",
},
"craft": "ISS",
The http://api.open-notify.org/astros.json
endpoint we used earlier does not take any
parameters. We just send a GET request
and the API sends back data about the
number of people currently in space.
parameters = {
"lat": 40.71,
"lon": -74
https://api.open-notify.org/iss-
pass.json?lat=40.71&lon;=-74
response = requests.get("https://api.open-notify.org/i
jprint(response.json())
{
"message": "success",
"request": {
"altitude": 100,
"datetime": 1568062811,
"latitude": 40.71,
"longitude": -74.0,
"passes": 5
},
"response": [
"duration": 395,
"risetime": 1568082479
},
"duration": 640,
"risetime": 1568088118
},
"duration": 614,
"risetime": 1568093944
},
"duration": 555,
"risetime": 1568099831
},
"duration": 595,
"risetime": 1568105674
}
Understanding the Pass Times
pass_times = response.json()['response']
jprint(pass_times)
[
"duration": 395,
"risetime": 1568082479
},
"duration": 640,
"risetime": 1568088118
},
"duration": 614,
"risetime": 1568093944
},
"duration": 555,
"risetime": 1568099831
},
"duration": 595,
"risetime": 1568105674
for d in pass_times:
time = d['risetime']
risetimes.append(time)
print(risetimes)
times = []
for rt in risetimes:
time = datetime.fromtimestamp(rt)
times.append(time)
print(time)
2019-09-09 21:27:59
2019-09-09 23:01:58
2019-09-10 00:39:04
2019-09-10 02:17:11
2019-09-10 03:54:34
What an API is
Types of requests and response codes
How to make a get request
How to make a request with parameters
How to display and extract JSON data
from an API
1 Take 75+ interactive courses
2 Apply your skills with projects
3 Join 1M+ learners
Start Now
API apis intermediate json NASA