LoginSignup
1
1

More than 1 year has passed since last update.

[Python] Requests パラメータ allow_redirects

Last updated at Posted at 2021-12-27

allow_redirects

Requestは(HEAD以外)デフォルトリダイレクトする

historyのプロパティでリダイレクトされたかの歴史を追跡できる

GitHub はHTTPHTTPSにリダイレクトする

default
>>> r = requests.get('http://github.com/')

>>> r.url
'https://github.com/'

>>> r.status_code
200

>>> r.history
[<Response [301]>]

allow_redirects=Falseを設定すると、リダイレクトをしない

allow_directs=False
>>> r = requests.get('http://github.com/', allow_redirects=False)

>>> r.status_code
301

>>> r.history
[]

HEADの場合、デフォルトはリダイレクトしない

リダイレクトをしたい場合、allow_redirects=Trueを設定すれば

allow_directs=True
>>> r = requests.head('http://github.com/', allow_redirects=True)

>>> r.url
'https://github.com/'

>>> r.history
[<Response [301]>]

参考

1
1
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
1