转自:http://www.weidianyuedu.com/content/3813436508720.html
六、关于重定向
有时候我们在请求url时,服务器会自动把我们的请求重定向,比如github会把我们的http请求重定向为https请求。我们可以使用r.history来查看重定向:
r = requests.get(‘http://pythontab.com/’)
r.url
‘http://pythontab.com/’r.history
[]
从上面的例子中可以看到,我们使用http协议访问,结果在r.url中,打印的却是https协议。那如果我非要服务器使用http协议,也就是禁止服务器自动重定向,该怎么办呢?使用allow_redirects 参数:
r = requests.get(‘http://pythontab.com’, allow_redirects=False)