Home > Net >  How to get correct website URL from a redirect in R?
How to get correct website URL from a redirect in R?

Time:12-17

Beginner here, I have a list (or rather column) full of website redirect URLs from which I want to get the "correct" website URL. Example, I have the URL https://icoholder.com/en/v2/ico/ico-redirect/4321?to=https://sirinlabs.com?utm_source=icoholder but I want to get the correct website URL https://sirinlabs.com/?utm_source=icoholder that appears in the search bar when you click the previous link and load the website.

Any idea how to manage this is in R for an entire column of these URLs?

Thanks in advance.

CodePudding user response:

You can use the httr library to get the final URL

url <- "https://icoholder.com/en/v2/ico/ico-redirect/4321?to=https://sirinlabs.com?utm_source=icoholder"
httr::GET(url)$url
# [1] "https://sirinlabs.com/?utm_source=icoholder"

That will actually make the HTTP request to see where the server sends you.

If you want to assume that the correct URL will always be in the ?to= querystring parameter, you can use

httr::parse_url(url)$query$to
[1] "https://sirinlabs.com?utm_source=icoholder"

without making any sort of HTTP request.

  • Related