我认为您可以使用 rvest 和 tidyverse 来做到这一点。
第一件事是将您的链接读取为带有read_html()的网址
library(tidyverse)
library(rvest)
url ="https://www.tutiempo.net/clima/06-2018/ws-830950.html"
data = read_html(url)
然后你必须用你感兴趣的信息来识别节点。在你使用html_nodes("table")的情况下,你可以看到这个网站有5个HTML表格,有趣的信息在第4个节点:
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 ...
一旦确定,您只需检索该节点:
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 …
然后你可以将你的数据框tab_temp保存为.csv,如果你想使用write.csv()或rio::export()。