0

Like we open a URL to a normal browser so it will redirect to another website url. Example a shorted link. After you open this it will redirect you to the main url.

So how to do this in python I mean I need to open a URL on python and this will redirect to other website page then I will copy the other website page link.

That's all I want to know thank you.

I tried it with python requests and urllib module.

Like this

import requests

a = requests.get("url", allow_redirects = True)

And

import urllib.request

a = urllib.request.urlopen("url")

But it's not working at all. I mean didn't get the redirected page.

1
  • there are 3 types of redirections. (1) server sends response with status 3xx and new address, (2) server sends HTML with tag <meta refresh="time;url"> (3) JavaScript can set document.location = url (or similar). Requests automatically redirect for first type. For second type It is easy to check if HTML has meta tag and run next request with new url. But third type makes problem because requests can't run JavaScript and there are many different method to assign new location. Commented Mar 31, 2022 at 5:37

1 Answer 1

2

I know 4 types of redirections.

  1. server sends response with status 3xx and new address

    HTTP/1.1 302 Found
    Location: https://new_domain.com/some/folder
    

    Wikipedia: HTTP 301, HTTP 302, HTTP 303

  2. server sends header Refresh with time in seconds and new address

    Refresh: 0; url=https://new_domain.com/some/folder
    
  3. server sends HTML with meta tag which emulates header Refresh

    <meta http-equiv="refresh" content="0; url=https://new_domain.com/some/folder">
    

    Wikipedia: meta refresh

  4. JavaScript sets new location

    location = url
    location.href = url
    location.replace(url)
    location.assing(url)
    

    The same for document.location, window.location

    There should be also combination with open(),document.open(), window.open()


requests automatically redirects for first and (probably) second type. With urllib probably you would have to check status, get url, and run next request - but this is easy. You can even run it in loop because some pages may have many redirections. You can test it on httpbin.org (even for multi-redirections)

For third type it is easy to check if HTML has meta tag and run next request with new url. And again you can run in loop because some pages may have many redirections.

But forth type makes problem because requests can't run JavaScript and there are many different methods to assign new location. They can also hide it in code - "obfuscation".


In requests you can check response.history to see executed redirections

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.