在 Python 3 中使用 urllib 重定向 url [英] Redirection url using urllib in Python 3
本文介绍了在 Python 3 中使用 urllib 重定向 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Python 3 中使用 urllib 跟踪重定向时,我需要知道我的最终 URL 是什么.
I would need to know what is my final URL when following redirections using urllib in Python 3.
假设我有一些代码,例如:
Let's say I've some code like :
opener = urllib.request.build_opener()
request = urllib.request.Request(url)
u = opener.open(request)
如果我的网址重定向到另一个网站,我怎么知道这个新网站的网址?我在文档中没有发现任何有用的东西.
If my urls redirects to another website, how can I know this new website URL ? I've found nothing useful in documentation.
感谢您的帮助!
推荐答案
你可以简单地使用
u.geturl()
获取您被重定向到的 URL(如果没有发生重定向,则为原始 URL).
to get the URL you were redirected to (or the original one if no redirect happened).
这篇关于在 Python 3 中使用 urllib 重定向 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文