python-gotosocial-notif/snippets.py

48 lines
829 B
Python
Raw Normal View History

2024-09-29 12:14:20 +02:00
# %%
import requests
import json
#import pandas as pd
api = 'https://jsonplaceholder.typicode.com/users'
req = requests.get(api)
nama = []
alamat = []
if req.status_code == 200:
data = req.json()
for d in data:
nama = d['name']
jalan = d['address']['street']
suite = d['address']['suite']
kota = d['address']['city']
print(f"nama: {nama} {jalan} {suite} ,{kota}")
else:
print(f'Gagal mengambil data')
# Python akses variable di dalam loop dari luar
if req.status_code == 200:
data = req.json()
n = []
k = []
for d in data:
nama = d['name']
kota = d['address']['city']
n.append(nama)
# j.append(jalan)
# s.append(suite)
k.append(kota)
print(f"{n} : {k}")
else:
print(f'Gagal mengambil data')