rexwashburn
1/25/2019 - 11:29 AM

APIからデータ取得

#!/usr/local/bin/python3.4
# coding:utf-8
import urllib.request
import json

print("Content-Type: application/json; charset=UTF-8\r\n")

url = 'https://jsonplaceholder.typicode.com/posts/'
req = urllib.request.Request(url)

with urllib.request.urlopen(req) as res:
  bytes = res.read()
  text = bytes.decode('utf-8')
  dump = json.loads(text)
  print(json.dumps(dump))