-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI_data.py
More file actions
33 lines (25 loc) · 910 Bytes
/
Copy pathAPI_data.py
File metadata and controls
33 lines (25 loc) · 910 Bytes
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
import requests
import json
# API endpoint (Example: Public API for Random Users)
url = "https://randomuser.me/api/?results=5"
# Fetch data from API
response = requests.get(url)
# Check if request was successful
if response.status_code == 200:
data = response.json()
print("API Data Fetched Successfully!\n")
# Print fetched data
print(json.dumps(data, indent=4))
# Process data (Extract only names and emails)
users = []
for user in data['results']:
users.append({
"name": f"{user['name']['first']} {user['name']['last']}",
"email": user['email']
})
# Save processed data to JSON
with open("users.json", "w",encoding="utf-8") as f:
json.dump(users, f, indent=4,ensure_ascii=False)
print("\nProcessed user data saved to users.json")
else:
print(f"Error: Unable to fetch data (Status Code: {response.status_code})")