4

I receive the following error when attempting to use the rvest package in R:

Error in open.connection(x, "rb") : Couldn't connect to server

What is causing this error message? The function is shown below:

htmlpage <- read_html("http://forecast.weather.gov/MapClick.php?lat=42.27925753000045&lon=-71.41616624299962#.V17UH-IrKHs")
lmo
  • 37,904
  • 9
  • 56
  • 69
N. Shep
  • 41
  • 1
  • 1
  • 3

3 Answers3

9

I tried different ways and the problem was not in the proxy connection, but in the way that R gets the connection. Through the definition of a binary connection of the url within url() the problem was solved.

con <- url("http://www.imdb.com/title/tt1490017/", "rb") 
lego_movie <- read_html(con)
OTStats
  • 1,820
  • 1
  • 13
  • 22
Tom Meallant
  • 91
  • 1
  • 3
1

You have to open a session and then read from it

htmlpage <- html_session("http://forecast.weather.gov/MapClick.php?lat=42.27925753000045&lon=-71.41616624299962#.V17UH-IrKHs", httr::user_agent("Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.20 (KHTML, like Gecko) Chrome/11.0.672.2 Safari/534.20"))

htmlpage %>%
  read_html() %>%
  html_text()
Trator
  • 11
  • 2
  • i originally was getting this error when I was on my HP ZBook work computer while in the office. when i connected my work computer to a hotspot, the issue went away. similarly, when i used my personal HP at home, i did not receive the same error. i am thinking the issue was caused by security settings at my company office. – N. Shep Jan 18 '17 at 17:56
0

The problem is that your work computer is always looking for your company's proxy server, and this happens whether or not you are at work, or using a hotspot, or even at home.

The easiest way to overcome this is to add a parameter atWork to your function and then use the httr::use_proxy() function to set the appropriate proxy to use:

myFunction <- function(arg1, atWork)

    if(atWork){
        proxy.string <- use_proxy("http://proxy-server.YourCompanyName.com", port = 8080)
    } else {
        proxy.string <- use_proxy("")
    }

# then open a session
sess <- html_session(myUrl, proxy.string)

do stuff here
return(result)
}

of course, you will need to replace proxy-server.YourCompanyName with your actual company's proxy server url.

hackR
  • 1,459
  • 17
  • 26