Skip to content

Commit 203871e

Browse files
nasa api wip
1 parent ee0f3c4 commit 203871e

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

source/reading.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,19 +1431,25 @@ responds to your request with some text:
14311431
{"date":"2023-07-13","explanation":"A mere 390 light-years away, Sun-like stars and future planetary systems are forming in the Rho Ophiuchi molecular cloud complex, the closest star-forming region to our fair planet. The James Webb Space Telescope's NIRCam peered into the nearby natal chaos to capture this infrared image at an inspiring scale. The spectacular cosmic snapshot was released to celebrate the successful first year of Webb's exploration of the Universe. The frame spans less than a light-year across the Rho Ophiuchi region and contains about 50 young stars. Brighter stars clearly sport Webb's characteristic pattern of diffraction spikes. Huge jets of shocked molecular hydrogen blasting from newborn stars are red in the image, with the large, yellowish dusty cavity carved out by the energetic young star near its center. Near some stars in the stunning image are shadows cast by their protoplanetary disks.","hdurl":"https://apod.nasa.gov/apod/image/2307/STScI-01_RhoOph.png","media_type":"image","service_version":"v1","title":"Webb's Rho Ophiuchi","url":"https://apod.nasa.gov/apod/image/2307/STScI-01_RhoOph1024.png"}
14321432
```
14331433

1434-
Woah! What a mess! There is definitely some data there, but it's a bit hard to see what it all is. As it turns out, this is a common format for data called *JSON* (JavaScript Object Notation). You can
1435-
interpret this data just like you'd interpret a Python dictionary: these are `key : value` pairs separated by commas. For example, if you look closely, you'll see that the first entry is
1436-
`"date":"2023-07-13"`, which indicates that we indeed successfully received data corresponding to July 13, 2023.
1437-
1438-
So now the job is to do all of this programmatically in Python. We will load the `requests` package to make the query, and the `json` package to interpret the JSON response.
1439-
You will recognize the same query URL we pasted into the browser below as well.
1434+
Woah! What a mess! There is definitely some data there, but it's a bit hard to
1435+
see what it all is. As it turns out, this is a common format for data called
1436+
*JSON* (JavaScript Object Notation). You can interpret this data just like
1437+
you'd interpret a Python dictionary: these are `key : value` pairs separated by
1438+
commas. For example, if you look closely, you'll see that the first entry is
1439+
`"date":"2023-07-13"`, which indicates that we indeed successfully received
1440+
data corresponding to July 13, 2023.
1441+
1442+
So now the job is to do all of this programmatically in Python. We will load
1443+
the `requests` package, and make the query using the `get` function, which takes a single URL argument;
1444+
you will recognize the same query URL that we pasted into the browser earlier below.
1445+
We will then name the response object `nasa_response`, and obtain a JSON representation using the `json` method.
1446+
Finally, we use the `dumps` method from the `json` package to obtain the response as a nicely-formatted string object.
14401447

14411448
```{code-cell} ipython3
14421449
:tags: [remove-output]
14431450
import requests, json
14441451
14451452
nasa_response = requests.get("https://api.nasa.gov/planetary/apod?api_key=YOUR_API_KEY&date=2023-07-13")
1446-
14471453
json.dumps(nasa_response.json(), indent=4, sort_keys=True)
14481454
```
14491455

0 commit comments

Comments
 (0)