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

编程入门 行业动态 更新时间:2024-10-27 06:21:15
本文介绍了如何使用 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

本文发布于:2023-10-10 12:05:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478623.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   重定向   Python   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!