【Python】HTTPリクエストを送信する(GET/POST)

python-logo HTTP

PythonでHTTPリクエストを送信する時に利用できるライブラリは2種類ある。

1urllib標準ライブラリやや使いにくい
2Requests標準ライブラリではない使いやすい

今回は、Requestsを使ったHTTPリクエストについて解説する。

インストール

以下のコマンドで、Requestsモジュールをインストールする。

pipなどを使う人もいると思うが、
私はモジュールをプロジェクトごとに管理しているので、pipenvを利用している。

$ pipenv install requests

Pipfileのpackagesにrequestsが追加されていることが分かる。

$ cat Pipfile
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
requests = "*"

[requires]
python_version = "3.8"

[scripts]
main = "python main.py"

いつもpipを使ってインストールしている場合は、以下のコマンドでインストールを行う。

$ pip install requests

使い方(GET)

使い方はとても簡単である。

結果を見やすく表示するために、pprintも利用している。

import requests
import pprint

# get()メソッドでGETリクエストを送信する
response = requests.get("https://jsonplaceholder.typicode.com/todos/1")

# 結果を出力する
pprint.pprint(response)


# 出力結果
# リクエスト結果のオブジェクトが取得できる
<Response [200]>

リクエスト結果が格納されたオブジェクトには、
値を取り出すための様々な関数が用意されている。

オブジェクトの中身をそのまま出力すると
どんな値を保持しているのかを確認することができる。

その際vars()関数を利用すると、オブジェクトの中身を見ることができる。

import requests
import pprint

# get()メソッドでGETリクエストを送信する
response = requests.get("https://jsonplaceholder.typicode.com/todos/1")

# 結果を出力する
pprint.pprint(vars(response))


# 出力結果
{'_content': b'{\n  "userId": 1,\n  "id": 1,\n  "title": "delectus aut autem",'
             b'\n  "completed": false\n}',
 '_content_consumed': True,
 '_next': None,
 'connection': <requests.adapters.HTTPAdapter object at 0x10bba42b0>,
 'cookies': <RequestsCookieJar[Cookie(version=0, name='__cfduid', value='d7b809ddf719e211e71f5ebdb0c2292a01597242064', port=None, port_specified=False, domain='.typicode.com', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1599834064, discard=False, comment=None, comment_url=None, rest={'HttpOnly': None, 'SameSite': 'Lax'}, rfc2109=False)]>,
 'elapsed': datetime.timedelta(microseconds=166231),
 'encoding': 'utf-8',
 'headers': {'Date': 'Wed, 12 Aug 2020 14:21:04 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Set-Cookie': '__cfduid=d7b809ddf719e211e71f5ebdb0c2292a01597242064; expires=Fri, 11-Sep-20 14:21:04 GMT; path=/; domain=.typicode.com; HttpOnly; SameSite=Lax', 'X-Powered-By': 'Express', 'X-Ratelimit-Limit': '10', 'X-Ratelimit-Remaining': '9', 'X-Ratelimit-Reset': '1593853875', 'Vary': 'Origin, Accept-Encoding', 'Access-Control-Allow-Credentials': 'true', 'Cache-Control': 'max-age=43200', 'Pragma': 'no-cache', 'Expires': '-1', 'X-Content-Type-Options': 'nosniff', 'Etag': 'W/"53-hfEnumeNh6YirfjyjaujcOPPT+s"', 'Via': '1.1 vegur', 'CF-Cache-Status': 'HIT', 'Age': '26476', 'cf-request-id': '0484a4d68200001d87d70f5200000001', 'Expect-CT': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"', 'Server': 'cloudflare', 'CF-RAY': '5c1ad7373d7c1d87-NRT', 'Content-Encoding': 'gzip'},
 'history': [],
 'raw': <urllib3.response.HTTPResponse object at 0x10bbd1160>,
 'reason': 'OK',
 'request': <PreparedRequest [GET]>,
 'status_code': 200,
 'url': 'https://jsonplaceholder.typicode.com/todos/1'}

簡単に説明すると

  • response.json():JSON形式でレスポンスボディを取得
  • response.status_code:ステータスコードを取得
  • response.text:レスポンスボディを普通に取得
  • response.headers:レスポンスヘッダを取得
  • response.url:リクエスト時のURLを取得

といった要領で様々なデータを取得することができる。

リクエストパラメータを指定する場合

リクエストパラメータは、辞書型でkey,valueを定義し、引数に入れるだけでOK。

自分で連結した文字列を生成する必要はない。

import requests
import pprint

# リクエストパラメータを辞書型で設定する
param = {'userId': 1}

# get()メソッドでGETリクエストを送信する
response = requests.get("https://jsonplaceholder.typicode.com/todos", params=param)

# リクエスト時のURLを出力する
pprint.pprint(response.url)


# 出力結果
'https://jsonplaceholder.typicode.com/todos/1?userId=1'

SSL証明書関連のエラーが発生した場合

以下のようなエラーが発生した場合は、verify=Falseを引数に指定することで解決する。

(Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1123)')))

恒久的な対応ではなく、あくまでも一時的な対応である。

import requests
import pprint

# get()メソッドでGETリクエストを送信する
response = requests.get("https://jsonplaceholder.typicode.com/todos/1", verify=False)

使い方(POST)

POSTの使い方もGETとほぼ同じである。

POSTの場合、リクエストヘッダやリクエストボディを設定するケースが多い。

その場合は、引数に指定するだけでOKである。

  • リクエストボディ:json=で指定
  • リクエストヘッダ:headers=で指定
import requests
import pprint

POST_URL = "https://jsonplaceholder.typicode.com/posts"

# リクエストボディを定義する
request_body = {'title': 'foo', 'body': 'bar', 'userId': 1}

# POSTリクエストを、リクエストボディ付きで送信する
response = requests.post(POST_URL, json=request_body)

# レスポンスボディを出力する
pprint.pprint(response.json())


# 出力結果
{'body': 'bar', 'id': 101, 'title': 'foo', 'userId': 1}

まとめ

  • PythonでHTTP送信を行うときは、requestsモジュールを使うと便利
  • get()でGETリクエストを簡単に送信
  • post()でPOSTリクエストを簡単に送信
  • リクエストボディやリクエストヘッダは引数に入れるだけ
  • レスポンスボディを見るときはjson()がおすすめ

タイトルとURLをコピーしました