如何使用 Python 获取重定向的 URL [英] How to get the URL of a redirect with Python

查看:71
本文介绍了如何使用 Python 获取重定向的 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中,我使用 urllib2 打开一个 url.此 url 重定向到另一个 url,该 url 重定向到另一个 url.

In Python, I'm using urllib2 to open a url. This url redirects to another url, which redirects to yet another url.

我希望在每次重定向后打印出网址.

I wish to print out the url after each redirect.

例如

-> = 重定向到

A -> B -> C -> D

A -> B -> C -> D

我想打印 B、C 和 D 的 URL(A 是已知的,因为它是起始 URL).

I want to print the URL of B, C and D (A is already known because it's the start URL).

推荐答案

可能最好的方法是子类化 urllib2.HTTPRedirectHandler.Dive Into Python 的有关重定向的章节可能会有所帮助.

Probably the best way is to subclass urllib2.HTTPRedirectHandler. Dive Into Python's chapter on redirects may be helpful.

这篇关于如何使用 Python 获取重定向的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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