본문 바로가기

내일배움 캠프/TIL

2023 05 23 선발대 숙제

 

import requests

url='https://jsonplaceholder.typicode.com/posts/'
data={
    'userId':1,
    'title':'homework',
    'body':'JeongSeungHo'
}
response = requests.post(url,json=data)
response = response.json()
title = response['title']
body = response['body']
userid = response['userId']
id = response['id']

file = open('result.txt','w')

file.write(f'title : {title}\n')
file.write(f'body :{body}\n')
file.write(f'userId :{userid}\n')
file.write(f'id : {id}\n')

file.close()

request.post요청을 통해 url과 data를 json형식으로 보내서 response를 받아온다음에 이 데이터를 각각의 변수에 담은 다음에 file=open을 통해서 쓰기모드로 result.txt를 연다음에 원하는 데이터를 넣고 close를 통해 닫으면서 저장을 하는 방식으로 문제를 풀었습니다