Python请求模块 - 在完成或错误时检索requests.history

时间:2016-10-26 22:32:13

标签: python python-2.7 redirect error-handling python-requests

我正在使用Python和请求模块来跟踪任何链接的重定向。我有以下代码:

import requests

r = requests.get("put link here")
print r.history 

此代码使我能够检索重定向的历史记录,这是我想要的。但是,有时候,从链接重定向时会出错:

# Sometimes links in the beginning, in between, or even in the end throws an error.
r = requests.get("put link here")

错误可能是由许多问题引起的。我是否有可能获得所有重定向,包括导致错误的链接?例如:

starting_link -> good_link1 -> good_link2 -> bad_link -> X (causes program to crash)

我想获得:

starting_link -> good_link1 -> good_link2 -> bad_link

请求模块可以实现吗?

谢谢!

示例:

# Good redirect since it will redirect you to https://www.yahoo.com (note the http -> https)
good_link = http://www.yahoo.com 

# Bad redirect
# Apologies, but I cannot provide you with an exact link. But it involves clicking a link that redirects you to "market://details?id=XXXXX"
# The requests module thinks that this is not a valid link (it is a valid 'link' in an Android phone)

0 个答案:

没有答案