简体   繁体   English

RVest R抓取-html_table()中缺少表格

[英]rvest R scraping - Missing tables from html_table()

I am beginner with rvest. 我是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. 我使用基于在线教程的代码,但是html_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 而且当我尝试使用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. 这个问题似乎是特定于此网站的,因为上面的代码适用于例如Wikipedia页面。

Solutions to similar problems posted on this website (eg removing comment tags) did not work for me. 解决此网站上发布的类似问题(例如删除评论标签)对我不起作用。

Any help would be most appreciated! 非常感激任何的帮助!

Works just fine without RSelenium or splashr: 没有RSelenium或splasher的情况下工作正常:

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>

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): 您可以使用RSelenium打开在其中生成页面的浏览器,然后使用rvest刮擦表(就像您所做的一样):

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM