I am using python requests library to send requests to websites, and I am only interested in getting the response headers. The problem that I am encountered is that if I use requests.head() method, and if the response is 30x redirect, the requests.head() method will not follow the redirection to the eventual website. While using requests.get() method does follow redirection, it is much slower than requests.head().
Here is an example:
>>> r = requests.head('http://google.com')
>>> r.status_code
301
>>> r = requests.get('http://google.com')
>>> r.status_code
200
Because google.com will redirect all http requests to https, if I send request to http://google.com, I will get a response to redirect to https://google.com.
So my question is if there is a way (without peeking into the response headers and manually following the 'Location' header) to let the requests.head() method follow the redirection?
I did many searches and did not find something that is really relevant.
here is what you need! check ref
>>> import requests
>>> r = requests.head('http://github.com/', allow_redirects=True)
>>> r.history
>>> [<Response [301]>]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With