如何检查如果URL重定向到使用Python另一个URL(How to check if the ur

2019-09-27 14:44发布

我要检查是否目标URL访问会后,被重定向。 我想我可以做这样的事情:

req = urllib2.Request(url=url, headers=headers)
resp = urllib2.urlopen(req, timeout=3)
code = resp.code
if code == '200': # valid
else: # not valid

但它不工作,因为即使URL重定向,我仍然得到200谁能帮助我PLZ?

Answer 1:

只是为了阐述我的评论:

req = urllib2.Request(url=url, headers=headers)
resp = urllib2.urlopen(req, timeout=3)
redirected = resp.geturl() != url # redirected will be a boolean True/False


文章来源: How to check if the url redirect to another url using Python