Notice
Recent Posts
Recent Comments
Link
아미(아름다운미소)
python json 송수신 본문
json은 URL요청시 입출력 데이터로 많이 활용합니다.
예제)
import json import urllib.request url = "http://www.naver.com" # URL d = {'name': '유키공', 'birth': '0703', 'age': 47} params = json.dumps(d).encode("utf-8") # encode: 문자열을 바이트로 변환 req = urllib.request.Request(url, data=params, headers={'content-type': 'application/json'}) response = urllib.request.urlopen(req) print(response.read().decode('utf8')) # decode: 바이트를 문자열로 변환위 예제는 http://www.naver.com 이라는 URL에 json요청을 보내고 그 응답으로 json을 리턴받아 출력하는 예제입니다. urllib.request.Request 사용시 json문자열이 아닌 json 바이트 배열로 주고 받아야 합니다.
'랭귀지 > python' 카테고리의 다른 글
python enumerate() (0) | 2018.09.23 |
---|---|
python eval (0) | 2018.09.22 |
Python 소스코드 인코딩 (0) | 2018.09.20 |
python anaconda에서 가상환경만들고 삭제하기 (0) | 2018.09.19 |
ImportError: No module named win32com.client (0) | 2018.09.18 |
Comments