How to get the URL of a redirect with Python
For Python 3, the solution with urllib
is much simpler:
import urllib
def resolve(url):
return urllib.request.urlopen(url).geturl()
You can easily get D by just asking for the current URL.
req = urllib2.Request(starturl, datagen, headers)
res = urllib2.urlopen(req)
finalurl = res.geturl()
To deal with the intermediate redirects you'll probably need to build your own opener, using HTTPRedirectHandler that records the redirects.
Probably the best way is to subclass urllib2.HTTPRedirectHandler
. Dive Into Python's chapter on redirects may be helpful.