1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PythonでWebAPIの呼び出し方を複数紹介します

Posted at

はじめに

PythonでAPIを呼び出す方法をご紹介します
今回使うAPIはAPI-FOOTBALLというサッカーの試合情報を取得できるAPIを使います
1日100リクエストまでであれば無料で使えるので、サッカーかAPIに興味ある方は使ってみてください

一覧

・requests
・http.client

requests

requests.py
import requests

url = "https://api-football-v1.p.rapidapi.com/v3/timezone"

headers = {
    "X-RapidAPI-Key": "YOUR_API_KEY",
    "X-RapidAPI-Host": "HOST_URI"
}

response = requests.get(url, headers=headers)
print(response.json())

http.client

httpClient.py
import http.client

conn = http.client.HTTPSConnection("api-football-v1.p.rapidapi.com")

headers = {
    'X-RapidAPI-Key': "YOUR_API_KEY",
    'X-RapidAPI-Host': "HOST_URI"
    }

conn.request("GET", "/v3/timezone", headers=headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))

http.clientの方はインストール無しで使えますが
requestsの方はインストールが必要ですが、コードがシンプルに書けるのでrequestsの方が好まれます。

補足

requestsのインストール方法

pip install requests

pipenvを使っている人は

pipenv install requests

Anacondaを使っている人は

conda install -c anaconda requests
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?