FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-data-basics/examples/03_api_request_basics.py at main · DataTideHH/python-data-basics · GitHub
DataTideHH
/
python-data-basics
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-data-basics
/
examples
/
03_api_request_basics.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
41 lines (30 loc) · 1.02 KB
Breadcrumbs
python-data-basics
/
examples
/
03_api_request_basics.py
Copy path
File metadata and controls
41 lines (30 loc) · 1.02 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""Basic public API request example.
This module calls Open-Meteo for Hamburg with Python standard-library tools.
The endpoint requires no API key, credentials or tokens.
"""
from
__future__
import
annotations
import
json
from
urllib
.
error
import
HTTPError
,
URLError
from
urllib
.
request
import
urlopen
OPEN_METEO_URL
=
(
"https://api.open-meteo.com/v1/forecast"
"?latitude=53.5503"
"&longitude=10.0007"
"¤t=temperature_2m,precipitation,cloud_cover,wind_speed_10m"
)
def
main
()
->
None
:
try
:
with
urlopen
(
OPEN_METEO_URL
,
timeout
=
15
)
as
response
:
raw_body
=
response
.
read
().
decode
(
"utf-8"
)
except
(
HTTPError
,
URLError
,
TimeoutError
)
as
exc
:
print
(
f"API request failed:
{
exc
}
"
)
return
data
=
json
.
loads
(
raw_body
)
print
(
"Top-level JSON keys:"
)
print
(
list
(
data
.
keys
()))
print
(
"
\n
Current weather values:"
)
current
=
data
.
get
(
"current"
, {})
for
key
,
value
in
current
.
items
():
print
(
f"
{
key
}
:
{
value
}
"
)
if
__name__
==
"__main__"
:
main
()
Back
|
FazBrowse Home
|
New Git URL