Reputation: 21
I am beginner with rvest. I am trying to scrape some tables from the website of the Italian Home Office. I use codes based on-line tutorials, but html_table fails do find any table. This happens when I try to get all the tables:
url <- "http://finanzalocale.interno.gov.it/apps/floc.php/certificati/index/codice_ente/1030491450/cod/4/anno/2014/md/0/cod_modello/CCOU/tipo_modello/U/cod_quadro/04"
webpage <- read_html(url)
tables <- html_table(webpage)
table1 <- tables[[1]]
and also when I try to get a single one using the Xpath
url <- "http://finanzalocale.interno.gov.it/apps/floc.php/certificati/index/codice_ente/1030491450/cod/4/anno/2014/md/0/cod_modello/CCOU/tipo_modello/U/cod_quadro/04"
table <- url %>%
read_html() %>%
html_nodes(xpath='//*[@id="center"]/div[3]/table') %>%
html_table()
table <- table[[1]]
head(table)
The issue seems to be specific to this website, because the above codes work on, for example, wikipedia pages.
Solutions to similar problems posted on this website (e.g. removing comment tags) did not work for me.
Any help would be most appreciated!
Upvotes: 2
Views: 854
Reputation: 78842
Works just fine without RSelenium or splashr:
library(rvest)
library(tidyverse)
pg <- read_html("http://finanzalocale.interno.gov.it/apps/floc.php/certificati/index/codice_ente/1030491450/cod/4/anno/2014/md/0/cod_modello/CCOU/tipo_modello/U/cod_quadro/04")
html_nodes(pg, "table.table-striped") %>%
purrr::map(html_table) %>%
purrr::map(as_tibble)
## [[1]]
## # A tibble: 75 x 11
## `FUNZIONI E SERVIZI / … Personale `Acquisto di beni … `Prestazioni di…
## <chr> <chr> <chr> <chr>
## 1 Funzioni generali di a… 141.967.7… 2.489.137,83 59.941.306,62
## 2 Organi istituzionali, … 16.229.61… 320.049,90 8.575.927,57
## 3 indennità per gli orga… 0,00 0,00 4.741.000,00
## 4 - Segreteria generale,… 40.013.84… 353.042,85 13.399.011,09
## 5 Gestione economica, fi… 7.925.580… 48.425,00 8.885.886,94
## 6 Gestione delle entrate… 7.630.190… 12.111,52 13.313.546,15
## 7 Gestione dei beni dema… 2.166.540… 1.759,99 1.233.312,68
## 8 Ufficio tecnico 14.404.02… 67.468,00 2.940.932,06
## 9 Anagrafe, stato civile… 16.826.86… 293.263,23 6.139.023,55
## 10 Altri servizi generali… 36.771.12… 1.393.017,34 5.453.666,58
## # ... with 65 more rows, and 7 more variables: `Utilizzo di beni di
## # terzi` <chr>, Trasferimenti <chr>, `Interessi passivi e oneri
## # finanziari diversi` <chr>, `Imposte e tasse` <chr>, `Oneri
## # straordinari della gestione corrente` <chr>, `Ammortamenti di
## # esercizio` <chr>, Totale <chr>
##
## [[2]]
## # A tibble: 75 x 11
## `FUNZIONI E SERVIZI / … Personale `Acquisto di beni … `Prestazioni di…
## <chr> <chr> <chr> <chr>
## 1 Funzioni generali di a… 131.219.7… 1.153.701,07 31.627.112,30
## 2 Organi istituzionali, … 15.658.15… 94.401,50 5.841.240,89
## 3 indennità per gli orga… 0,00 0,00 3.769.143,70
## 4 Segreteria generale, p… 38.448.56… 167.482,23 7.507.263,62
## 5 Gestione economica, fi… 7.662.138… 29.961,40 6.347.472,21
## 6 Gestione delle entrate… 7.487.402… 7.671,92 2.497.130,97
## 7 Gestione dei beni dema… 2.100.595… 1.759,99 852.589,53
## 8 Ufficio tecnico 11.334.26… 47.329,00 1.568.994,43
## 9 Anagrafe, stato civile… 16.144.91… 223.148,43 4.077.270,69
## 10 Altri servizi generali… 32.383.70… 581.946,60 2.935.149,96
## # ... with 65 more rows, and 7 more variables: `Utilizzo di beni di
## # terzi` <chr>, Trasferimenti <chr>, `Interessi passivi e oneri
## # finanziari diversi` <chr>, `Imposte e tasse` <chr>, `Oneri
## # straordinari della gestione corrente` <chr>, `Ammortamenti di
## # esercizio` <chr>, Totale <chr>
##
## [[3]]
## # A tibble: 74 x 11
## `FUNZIONI E SERVIZI / … Personale `Acquisto di beni … `Prestazioni di…
## <chr> <chr> <chr> <chr>
## 1 Funzioni generali di a… 5.593.882… 2.190.159,89 22.691.402,00
## 2 Organi istituzionali, … 463.851,43 1.649.220,45 2.549.386,60
## 3 indennità per gli orga… 0,00 0,00 554.624,70
## 4 Segreteria generale, p… 1.312.740… 47.613,00 8.238.186,49
## 5 Gestione economica, fi… 227.143,48 12.365,99 2.101.177,76
## 6 Gestione delle entrate… 220.687,71 16.698,92 3.981.304,84
## 7 Gestione dei beni dema… 62.536,51 293,70 255.524,99
## 8 Ufficio tecnico 1.933.388… 27.547,69 1.236.148,00
## 9 Anagrafe, stato civile… 394.806,86 69.201,92 1.971.067,96
## 10 Altri servizi generali 978.727,38 367.218,22 2.358.605,36
## # ... with 64 more rows, and 7 more variables: `Utilizzo di beni di
## # terzi` <chr>, Trasferimenti <chr>, `Interessi passivi e oneri
## # finanziari diversi` <chr>, `Imposte e tasse` <chr>, `Oneri
## # straordinari della gestione corrente` <chr>, `Ammortamenti di
## # esercizio` <chr>, Totale <chr>
Upvotes: 2
Reputation: 673
You can use RSelenium
to open a browser in which the page is generated and then scrape the tables with rvest
(just like you were doing):
library(rvest)
library(RSelenium)
rD <- rsDriver()
remDr <- rD[["client"]]
remDr$navigate("http://finanzalocale.interno.gov.it/apps/floc.php/certificati/index/codice_ente/1030491450/cod/4/anno/2014/md/0/cod_modello/CCOU/tipo_modello/U/cod_quadro/04")
webpage <- read_html(remDr$getPageSource()[[1]])
tables <- webpage %>% html_table(fill=T)
Upvotes: 1