Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I follow any redirections of a url in R?

Tags:

r

rcurl

httr

Suppose I have the following url:

http://linkinghub.elsevier.com/retrieve/pii/S1755534516300379

When entering this into my standard desktop browser, I get redirected to:

http://www.sciencedirect.com/science/article/pii/S1755534516300379?via%3Dihub

However, I am not able to implement this in R. I tried the packages httr and RCurl. In the documentation of httr, it says the function GET used as follows:

library(httr)
GET("http://linkinghub.elsevier.com/retrieve/pii/S1755534516300379")

is supposed to lead to the actual url used (after any redirects). But when calling the url:

GET("http://linkinghub.elsevier.com/retrieve/pii/S1755534516300379")$url

I don't get the final redirection. I would very much appreciate your help!

like image 824
Patrick Balada Avatar asked Jan 31 '26 05:01

Patrick Balada


1 Answers

For future reference, here is a little code snippet I wrote to follow a redirect using HEAD (instead of GET, so to not download more than needed). It will not work for the question at hand, but may help people in the future (with simpler scenarios).

# FUNCTIONS
url_after_redirect_1 <- function(url) {
  library(httr)
  a <- HEAD(url)
  # headers(a)
  (a$all_headers[[2]])$headers$location  
}
url_after_redirect <- Vectorize(url_after_redirect_1)
like image 196
Tal Galili Avatar answered Feb 02 '26 20:02

Tal Galili



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!