Redirection url using urllib in Python 3

て烟熏妆下的殇ゞ 提交于 2019-12-19 20:47:32

问题


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.

Thanks for your help !


回答1:


You can simply use

u.geturl()

to get the URL you were redirected to (or the original one if no redirect happened).



来源:https://stackoverflow.com/questions/4946244/redirection-url-using-urllib-in-python-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!