KnowHow

技術的なメモを中心にまとめます。
検索にて調べることができます。

pythonでサードパーティを使わずAPIにリクエストする

登録日 :2025/01/26 20:13
カテゴリ :Python基礎

urllib.requestを使うと、サードパーティのrequestを使わずAPIにリクエストをすることができる。

#!/usr/bin/python3

import urllib.request
import json


def request_get(_url):
    with urllib.request.urlopen(_url) as f:
        #print(f.read().decode('utf-8'))
        r = f.read().decode('utf-8')
        d = json.loads(r)
        print(r)
        print(d)
        print(type(d))


if __name__ == '__main__':

    url = 'http://httpbin.org/get'
    print(url)

    request_get(url)