rvest:select 并在特定(标题)字符串后刮取 html table(s)

rvest: select and scrape html table(s) after a specific (title) string

我正在尝试使用 public 医疗保健数据抓取以下网站: https://prog.nfz.gov.pl/app-jgp/Grupa.aspx?id=Qpc6nYOpOBQ%3d

我只想抓取前面有标题的 table 或 table "Tabela xx procedury ICD-9" 其中 xx 不是固定数字。

页面上可能有 1 个,但偶尔会有 2-3 个这样的 table。而且它们可以与其他页面以不同的顺序出现,因此无法表示我想要网站上的第 n 页。

例如,我只对同一页面中以 "Icd-9 main" 字符串开头的 table 感兴趣,并跳过其他。有 2 个这样的 table,我想将它们的内容抓取到 data.frame。它们的出现顺序可能与下面不同,因此我必须依赖前面的字符串。偶尔会完全没有Icd-9 main table.

Page
----
Icd-10
====
Table
====

Icd-9 main
====
Table
===

Icd-9 main
====
Table
====


Icd-9 supplementary
====
Table
===

我只知道 select 第 n 个 table 的代码,如本教程中的顺序:

https://www.r-bloggers.com/using-rvest-to-scrape-an-html-table/

library("rvest")
url <- "http://en.wikipedia.org/wiki/List_of_U.S._states_and_territories_by_population"
population <- url %>%
  html() %>%
  html_nodes(xpath='//*[@id="mw-content-text"]/table[1]') %>%
  html_table()
population <- population[[1]]

因此所有 table 都被转储到一个列表中,然后我们可以 select table 我们想要的数字

但是在我的例子中,我永远不知道要抓取哪个 table,顺序可能会有所不同,它可以是 2 table 前面有一个包含 "Tabela xx: procedury ICD-9"[=17= 的字符串]

我的问题是,如何根据预先确定的标题或描述字符串 select 和抓取 html table。或者 table 或 tables 仅出现在包含例如 "Tabela xx procedury ICD-9"

的字符串之后
library(rvest)
library(stringr)

doc <- read_html("https://prog.nfz.gov.pl/app-jgp/Grupa.aspx?id=Qpc6nYOpOBQ%3d")

# extract all the nodes that have the title (id = "tytul") or a table
# the cs selector "," is like a boolean OR. 
nodes <- doc %>% html_nodes(".tytul,table")

# loop though each node.
signal <- FALSE
my_tables <- list()
j <- 0
for (i in 1:length(nodes)) {

  # if title signal previously set and this is a table tag
  if (signal & html_name(nodes[i]) == "table") {
    cat("Match..\n")

    # get the table (data frame)
    this_table <- html_table(nodes[i], fill = TRUE, header = TRUE)[[1]]

    # append to list
    j = j + 1
    my_tables[[j]] <- this_table

    # and reset the signal so we search for the next one
    signal <- FALSE
  }

  # if the signal is clear look for matching title
  if (!signal) {
    signal <- nodes[i] %>% html_text() %>% str_detect("Tabela.+ICD 9")
  }
}
my_tables[[1]][1:5,]
my_tables[[2]][1:5,]

# > my_tables[[1]][1:5,]
# ICD 9                                                    Nazwa Lb. hospitalizacji UdziaĹ\u0082 (%) Mediana czasu pobytu (dni)
# 1 2.051       ZaĹ\u0082oĹźenie pĹ\u0082ytki sztucznej do czaszki                168            32,31                          7
# 2 1.247 Kraniotomia z usuniÄ\u0099ciem krwiaka podtwardĂłwkowego                 55            10,58                         20
# 3 2.022                       Odbarczenie zĹ\u0082amania czaszki                 43             8,27                          6
# 4 2.040                 Przeszczep kostny do koĹ\u009bci czaszki                 35             6,73                          8
# 5 1.093                        Inne aspiracje w zakresie czaszki                 33             6,35                          5
# > my_tables[[2]][1:5,]
# ICD 9                                         Nazwa Lb. hospitalizacji UdziaĹ\u0082 (%) Mediana czasu pobytu (dni)
# 1    O35                                     SĂłd (Na)                239            45,96                          8
# 2  89.00          Porada lekarska, konsultacja, asysta                230            44,23                          9
# 3    N45                                     Potas (K)                217            41,73                          8
# 4 87.030                  TK gĹ\u0082owy bez kontrastu                214            41,15                          9
# 5  89.04 Opieka pielÄ\u0099gniarki lub poĹ\u0082oĹźnej                202            38,85                          8