1
votes

I'm trying to scrape a table from a website with rvest in R, but an error appears:

"Error in open.connection(x, "rb") : HTTP error 404"

How can I solve the problem?

I've already tried a few options but no of them worked. I don't know what I'm doing wrong. I want to know if I'm coding in the wrong way. It seems to be something very simple but I can't solve it.

options(stringsAsFactors = FALSE)

url<- "https://www.tutiempo.net/clima/06-2018/ws-830950.html"

tbl.names <- data.frame(
  "Temperatura Media" = character(0),
  "Temperatura Máxima" = character(0),
  "Temperatura Mínima" = character(0),
  "SLP" = character(0),
  "H" = character(0),"PP" = character(0),
  "VV" = character(0),
  "V" = character(0),"VM" = character(0),
  "VG" = character(0),
  "RA" = character(0),"SN" = character(0),
  "TS" = character(0),
  "FG" = character(0)  )

for (i in 1:31) {
  url <- paste0(url, i)
  tbl.page <- url %>%
    read_html() %>%
    html_nodes(xpath='//*[@id="ColumnaIzquierda"]/div/div[4]') %>%
    html_table()
  names(tbl.page[[1]]) <- names(tbl.names)
  tbl.names <- bind_rows(tbl.names, tbl.page[[1]])
}

I'm expecting to get back the same table that is on the website but in a .xls or .csv file, so I can manipulate it. This error appears

"Error in open.connection(x, "rb") : HTTP error 404".

Although, I don't know if the coding is ok.

2

2 Answers

1
votes

I think you can do it With rvest and tidyverse.

The first thing is to read your link as a url with read_html()

library(tidyverse)
library(rvest)

url ="https://www.tutiempo.net/clima/06-2018/ws-830950.html" 


data = read_html(url)

Then you have to identify the nodes with the information you are interested in. In your case using html_nodes("table") you can see that in this website there are 5 HTML tables and the interesting information is in 4th node:

data %>%
   html_nodes("table")

{xml_nodeset (5)}
[1] <table cellpadding="0" cellspacing="0"><tbody><tr>\n<td class="home"><a href="https://www.tutiempo.net/">El tiempo</a></td>\n<td><a href=" ...
[2] <table cellpadding="0" cellspacing="0"><tr>\n<td><a href="/clima/ws-830950.html">Clima</a></td>\n\t\t\t\t\t<td><a href="/clima/2018/ws-830 ...
[3] <table cellpadding="0" cellspacing="0"><tr>\n<td><span class="social facebook iw-facebook" title="Compartir en Facebook"></span></td>\n<td ...
[4] <table cellpadding="0" class="medias mensuales numspan" style="width:100%;" cellspacing="0">\n<tr>\n<th>Día</th>\n<th><abbr class="tooltip ...
[5] <table cellpadding="0" cellspacing="0" class="info">\n<tr>\n<td>T</td>\n<td>Temperatura media (°C)</td>\n</tr>\n<tr>\n<td>TM</td>\n<td>Tem ...

Once identified, you just need to retrieve that node:

tab_temp = data %>%
  html_nodes("table") %>%
  .[4] %>%
  html_table(fill = TRUE) %>%
  as.data.frame()

glimpse(tab_temp)
Observations: 32
Variables: 15
$ Día <chr> "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "…
$ T   <chr> "26.1", "25.1", "26.7", "", "", "", "25.7", "24.7", "26.5", "", "", "", "27.2", "26.1", "26.6", "", "", "", "25.2", "26.6", "26.3"…
$ TM  <chr> "28", "28", "29", "", "", "", "28", "28", "29", "", "", "", "29", "27", "29", "", "", "", "29", "29", "29", "", "", "", "29", "27"…
$ Tm  <chr> "24", "23", "24", "", "", "", "23", "23", "23", "", "", "", "23", "23", "23", "", "", "", "23", "25", "23", "", "", "", "22", "20"…
$ SLP <chr> "-", "-", "-", "", "", "", "-", "-", "-", "", "", "", "-", "-", "-", "", "", "", "-", "-", "-", "", "", "", "-", "-", "-", "", "",…
$ H   <chr> "74", "89", "82", "", "", "", "85", "90", "82", "", "", "", "68", "74", "70", "", "", "", "77", "68", "70", "", "", "", "68", "83"…
$ PP  <chr> "-", "-", "-", "", "", "", "-", "-", "0", "", "", "", "0", "-", "-", "", "", "", "-", "0", "0", "", "", "", "-", "-", "-", "", "",…
$ VV  <chr> "9.7", "8.9", "10", "", "", "", "9", "8.7", "10", "", "", "", "10", "10", "9.8", "", "", "", "9.7", "10", "10", "", "", "", "9.7",…
$ V   <chr> "11.3", "9.1", "13.7", "", "", "", "8.5", "7.2", "10.6", "", "", "", "19.8", "15.9", "18.1", "", "", "", "16.7", "19.3", "18", "",…
$ VM  <chr> "18.3", "33.5", "24.1", "", "", "", "22.2", "16.5", "18.3", "", "", "", "29.4", "31.7", "29.4", "", "", "", "29.4", "24.1", "25.9"…
$ VG  <chr> "-", "-", "-", "", "", "", "-", "-", "-", "", "", "", "-", "-", "-", "", "", "", "-", "-", "-", "", "", "", "-", "-", "-", "", "",…
$ RA  <chr> "o", "o", "o", "o", "", "o", "o", "o", "", "", "", "", "", "o", "o", "o", "o", "o", "o", "", "", "o", "", "", "o", "o", "o", "", "…
$ SN  <chr> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Medias y …
$ TS  <chr> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Medias y …
$ FG  <chr> "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Medias y …

Then you can save your data frame tab_temp as .csv if you want with write.csv() or rio::export().

1
votes

I would go with a similar approach to other answer but as the table can be uniquely identified by class, and class is the second fastest css selector method, I would retrieve a single node (the table), and work with that by matching on class

library(rvest)

url ="https://www.tutiempo.net/clima/06-2018/ws-830950.html" 
df = read_html(url) %>% html_node(".mensuales") %>% html_table(fill = TRUE) %>% as.data.frame()