I was trying redirects but the result isn't what I was exprecting and I was wondering why
When I request github.io
, I get 301 in resp.history, but when I check r.is_redirect
, it return False
headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"}
url = "https://github.io"
r = requests.get(url, headers=headers)
print(r.history)
for i in r.history:
print(i.url)
print(r.is_redirect)
[out]:
[<Response [301]>] # r.history
https://github.io/ # loop for r.history urls
False # r.is_redirect
Also what is a 'prepared_request' in Requests, is it like the request.Request()
obj in urllib? If so, when and why should I use it, since unlike urllib, we can pass in params and headers etc into request.get()
And, what is the difference between resp.url
and resp.request.url
? is resp.request.url
the initial url I requested in get()? Then why I'm getting the redirected url https://pages.github.com/
, with the above code, when I try print(r.request.url)
question from:
https://stackoverflow.com/questions/65857816/some-questions-on-redirects-for-requests-library 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…