Autosave: 2024-06-19 07:30:05
This commit is contained in:
parent
ddb3c103e2
commit
fd9af49c7e
2 changed files with 5 additions and 7 deletions
BIN
.zk/notebook.db
BIN
.zk/notebook.db
Binary file not shown.
|
@ -8,8 +8,7 @@ tags:
|
||||||
# Making network requests in Python
|
# Making network requests in Python
|
||||||
|
|
||||||
We can use the `requests` package to make API requests to
|
We can use the `requests` package to make API requests to
|
||||||
[RESTful](RESTful_APIs.md) resources and handle the data as
|
[RESTful](RESTful_APIs.md) resources and handle the data as JSON.
|
||||||
JSON.
|
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
pip install requests
|
pip install requests
|
||||||
|
@ -17,7 +16,7 @@ pip install requests
|
||||||
|
|
||||||
Here is a basic architecture for making a `GET` request in Python.
|
Here is a basic architecture for making a `GET` request in Python.
|
||||||
|
|
||||||
```py
|
```python
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
def get_data(url):
|
def get_data(url):
|
||||||
|
@ -46,7 +45,7 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
Running `main` returns:
|
Running `main` returns:
|
||||||
|
|
||||||
```py
|
```python
|
||||||
{
|
{
|
||||||
"id":1,
|
"id":1,
|
||||||
"title":"iPhone 9",
|
"title":"iPhone 9",
|
||||||
|
@ -68,13 +67,12 @@ Running `main` returns:
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
This is JSON but in Python is a
|
This is JSON but in Python is a [dictionary](Dictionaries_in_Python.md)
|
||||||
[dictionary](Dictionaries_in_Python.md)
|
|
||||||
|
|
||||||
We can use standard dictionary methods to handle the data. For example, we'll
|
We can use standard dictionary methods to handle the data. For example, we'll
|
||||||
add to the existing `try` block:
|
add to the existing `try` block:
|
||||||
|
|
||||||
```py
|
```python
|
||||||
example_key = "brand" # Replace with the key you want to access from the JSON data
|
example_key = "brand" # Replace with the key you want to access from the JSON data
|
||||||
if example_key in data:
|
if example_key in data:
|
||||||
print(f"Value of '{example_key}':", data[example_key])
|
print(f"Value of '{example_key}':", data[example_key])
|
||||||
|
|
Loading…
Add table
Reference in a new issue