0
import requests
print(requests.get("http://zfxxgk.nea.gov.cn/2022-01/17/c_1310427545.htm").url)
#returns 'http://zfxxgk.nea.gov.cn/2022-01/17/c_1310427545.htm'

Above code returns the same url as I passed, but the url actually redirect to the other url. How can I get the after redirected url? Thank you

6
  • I know 4 methods to redirect page - HTTP 3xx, 2 methods HTML with tag <meta>, and JavaScript window.location = new_url - and requests can redirect only HTTP 3xx Commented Apr 7, 2022 at 7:38
  • are you sure it redirect page? Maybe it uses JavaScript to load new content without redirection. When I load this page in browser then I always see the same URL - but I don't know what is on page because I don't know chinese. Commented Apr 7, 2022 at 7:41
  • Thank you for your comments, furas! I typed redirected url in my code wrongly. Now I fixed it to 'zfxxgk.nea.gov.cn/2022-01/17/c_1310427545.htm' Commented Apr 7, 2022 at 8:39
  • How should i get another redirected page url in python Commented Apr 7, 2022 at 9:40
  • 1
    Thank you guys. I finnaly successed getting redirected page url. I could find it in <script> tag which excutes the javascript to redirect. I use beautifulsoup to fetch it. Commented Apr 7, 2022 at 9:48

1 Answer 1

1

with a curl-call

curl  http://zfxxgk.nea.gov.cn/2022-01/17/c_1310427545.htm

you get the source witch contains the javascript-redirect as furas mentioned:

<script language="javascript" type="text/javascript">window.location.href="http://zfxxgk.nea.gov.cn/2021-12/31/c_1310427545.htm";</script>

If you got a "real" redirection you will find the new "location" in the header:

curl -I  http://something.somewhere

HTTP/1.1 301 Moved Permanently
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: https://inder.net

If you need it an automation with python you should look at request.history as described in this answer to a similar question to find all redirections your call triggered: "Python Requests library redirect new url"

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.