2011-06-17 26 views
1

我想爲例如 - www.xyz.compython - 打開url並檢索已更改的url

它點擊後在URL欄上產生新的URL後的身份驗證網址,例如www.xyz-11.com

如何從python中檢索這個?
調用一個網址,並獲得新創建的網址?

+1

哪些庫是你想使用? `urllib2`? `httplib`?還有別的嗎?請指定更多,或者如果您根本不知道要使用什麼,請嘗試四處搜索。 – 2011-06-17 11:45:41

+0

我使用urllib2 – sam 2011-06-17 11:46:33

回答

6
>>> import urllib2 
>>> u = urllib2.urlopen('http://google.com') 
>>> dir(u) # useful in seeing what's there, see also help(u) 
['__doc__', '__init__', '__iter__', '__module__', '__repr__', 'close', 'code', 'fileno', 'fp', 'getcode', 'geturl', 'headers', 'info', 'msg', 'next', 'read', 'readline', 'readlines', 'url'] 
>>> u.geturl() 
'http://www.google.com.au/' 
>>> u.url 
'http://www.google.com.au/' 

另請參閱the documentation for urllib2.urlopen;

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