Home > OS >  Python - Resolve response Remote URL link with actual page
Python - Resolve response Remote URL link with actual page

Time:02-05

I am trying to get the actual page URL using the remote URL from JSON response. Below is the remote URL I get in an JSON API response.

https://somesite.com/mainlink/1eb-68a8-40be-a3-5679e/utilities/927-40-b958-3b5?pagePath=teststaff

when I click the link, it resolves to actual page when opened in browser which in this case is

https://somesite.com/mainlink/recruitement/utilities/salesteam?pagePath=teststaff

How can programatically get this resolved URL without opening in browser ?

CodePudding user response:

IIUC, use requests.head with allow_redirects=True to ask for the final URL :

import requests
​
url = "https://en.wikipedia.org/"
​
infos = requests.head(url, allow_redirects=True)
​

Output : ​

print(infos.url)
​#https://en.wikipedia.org/wiki/Main_Page
  • Related