如何使用 Python 获取重定向的 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4902523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 18:02:56  来源:igfitidea点击:

How to get the URL of a redirect with Python

pythonredirecturllib2

提问by Matthew H

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

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

I wish to print out the url after each redirect.

我希望在每次重定向后打印出 url。

For example

例如

-> = redirects to

-> = 重定向到

A -> B -> C -> D

A -> B -> C -> D

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

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

采纳答案by Wooble

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

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

回答by chmullig

You can easily get D by just asking for the current URL.

您只需询问当前 URL 即可轻松获得 D。

req = urllib2.Request(starturl, datagen, headers)
res = urllib2.urlopen(req)
finalurl = res.geturl()

To deal with the intermediate redirects you'll probably need to build your own opener, using HTTPRedirectHandlerthat records the redirects.

要处理中间重定向,您可能需要使用记录重定向的HTTPRedirectHandler构建自己的 opener

回答by jadelord

For Python 3, the solution with urllibis much simpler:

对于 Python 3,解决方案urllib要简单得多:

import urllib


def resolve(url):
    return urllib.request.urlopen(url).geturl()