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

Light
  • 31
  • 5
  • I know 4 methods to redirect page - HTTP 3xx, 2 methods HTML with tag ``, and JavaScript `window.location = new_url` - and `requests` can redirect only `HTTP 3xx` – furas Apr 07 '22 at 07: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. – furas Apr 07 '22 at 07:41
  • Thank you for your comments, furas! I typed redirected url in my code wrongly. Now I fixed it to 'http://zfxxgk.nea.gov.cn/2022-01/17/c_1310427545.htm' – Light Apr 07 '22 at 08:39
  • [How should i get another redirected page url in python](https://stackoverflow.com/questions/71686502/how-should-i-get-another-redirected-page-url-in-python/71687393#71687393) – furas Apr 07 '22 at 09:40
  • if page use JavaScript to redirect page then you may need to search `location = ` in HTML. OR you may have to use [Selenium](https://selenium-python.readthedocs.io/) to control real web browser which can run JavaScript. – furas Apr 07 '22 at 09:43
  • 1
    Thank you guys. I finnaly successed getting redirected page url. I could find it in – Light Apr 07 '22 at 09:48

1 Answers1

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"

Karl
  • 39
  • 5