有没有一种简单的方法来在python中请求URL而不是遵循重定向? [英] Is there an easy way to request a URL in python and NOT follow redirects?

查看:99
本文介绍了有没有一种简单的方法来在python中请求URL而不是遵循重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看urllib2的来源,看起来最简单的方法就是将HTTPRedirectHandler子类化,然后使用build_opener来覆盖默认的HTTPRedirectHandler,但这似乎很多(相对复杂)的工作做的似乎是它应该很简单。

Looking at the source of urllib2 it looks like the easiest way to do it would be to subclass HTTPRedirectHandler and then use build_opener to override the default HTTPRedirectHandler, but this seems like a lot of (relatively complicated) work to do what seems like it should be pretty simple.

推荐答案

这是请求方式:

import requests
r = requests.get('http://github.com', allow_redirects=False)
print(r.status_code, r.headers['Location'])

这篇关于有没有一种简单的方法来在python中请求URL而不是遵循重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆