0

I'm trying to get a redirected url from another url without using a selenium object. I have an url like:

     http://registry.theknot.com/track/View?lt=RetailerGVR&r=325404419&rt=12160&a=994&st=RegistryProfile&ss=LinkedRegistries&sp=Logo

and it gets redirected to:

     http://www.target.com/RegistryGiftGiverCmd?isPreview=false&status=completePageLink&registryType=WD&isAjax=false&listId=NjPO_i-DoIafZPZSFhaBRw&clkid=2gTTqGRwsXS4x%3AexW%3ATGBxiqUkWXSi0It0P5VM0&lnm=Online+Tracking+Link&afid=The+Knot%2C+Inc.+and+Subsidiaries&ref=tgt_adv_xasd0002

when is opened by some browser.

I want to avoid instancing a Selenium object and raise a Firefox/Chrome process just to get the redirected URL. Is there any other better way?

Thanks!

1 Answer 1

2

If this is just an HTTP redirect, urllib.request/urllib2 in the standard library can follow redirects just fine, as can third-party HTTP client libraries like requests and PycURL. In fact, in the simplest use cases, they do so automatically.

So, just:

>>> import urllib.request
>>> original_url = 'http://registry.theknot.com/track/View?lt=RetailerGVR&r=325404419&rt=12160&a=994&st=RegistryProfile&ss=LinkedRegistries&sp=Logo'
>>> u = urllib.request.urlopen(original_url)
>>> print(u.url)
http://www.target.com/RegistryGiftGiverCmd?isPreview=false&status=completePageLink&registryType=WD&isAjax=false&listId=NjPO_i-DoIafZPZSFhaBRw&clkid=0b5XTmU%3A5WbqRETSYD20AQKOUkWXSGQgQSquVU0&lnm=Online+Tracking+Link&afid=The+Knot%2C+Inc.+and+Subsidiaries&ref=tgt_adv_xasd0002

But if you just want the data, you don't even need that:

>>> data = u.read()

That's the contents of the redirected request.

(For Python 2.x, just replace urllib.request with urllib2 and it works the same.)


The only reason you'd need to use Selenium (or another browser automation and/or JS-environment library) is if the redirect is done through in-page JavaScript. Which it usually isn't, and isn't in this case. There's no reason to go outside the standard library, talk to another app, etc. for simple things like this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.