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()
.