import requests
try:
response = requests.get("https://jsonplaceholder.typicode.com/posts")
response.raise_for_status()
posts = response.json()[:10]
except requests.exceptions.RequestException as e:
print(f"API'den veri alınırken hata oluştu: {e}")
posts = None
if posts:
try:
with open("posts.txt", "w") as file:
for post in posts:
file.write(f"userId = {post['userId']}, id = {post['id']}, title = {post['title']}, completed = {post['completed']}\n")
except FileNotFoundError:
print("Dosya bulunamadı.")
except PermissionError:
print("Dosyaya yazma izniniz yok.")
except ValueError:
print("Beklenmeyen veri formatı hatası.")
except Exception as e:
print(f"Beklenmeyen bir hata oluştu: {e}")
else:
print("Veri başarıyla dosyaya kaydedildi.")
finally:
print("Dosya yazma işlemi tamamlandı.")