2010-08-24 54 views

回答

31

調用返回的文件對象的.geturl()方法。每urllib2 docs

geturl() - 返回檢索到的資源的URL,常用來確定是否重定向之後

例子:

import urllib2 
response = urllib2.urlopen('http://tinyurl.com/5b2su2') 
response.geturl() # 'http://stackoverflow.com/'
+0

如何當有多箇中間的網址,我想最終網址處理?這不適用於這種情況。 – Kishan 2016-11-29 08:58:42

4

urllib2.urlopen的返回值一個geturl()方法,它應該返回實際的(即上次重定向)url。

1

例如爲: urllib2.urlopen('ORIGINAL LINK').geturl()

urllib2.urlopen(urllib2.Request('ORIGINAL LINK')).geturl()

相關問題