警告 xml_find_all.xml_node while webscrape with rvest

warning xml_find_all.xml_node while webscrape with rvest

我想从这个 link 中抓取数据。我使用 rvest 和一个循环来获取 tables。

这是我的代码:

require(dplyr)
require(rvest)

# store web url
url <- "https://corona.thueringen.de/covid-19-bulletin/"
# check xpath
xpath_part1="/html/body/main/div[2]/div/section[2]/ul/li["
xpath_part2="]/div/div/div[2]/div/table"

# save tables
for(i in 1:50){
  tbl_test_ <- 
    url %>% 
    xml2::read_html() %>%
    rvest::html_nodes(xpath=paste0(xpath_part1, i, "]", xpath_part2)) %>%
    rvest::html_table(fill = TRUE) %>%
    dplyr::rename(Gesundheitsamt=1)
}

我收到以下错误:

Fehler in UseMethod("rename_") : 
  nicht anwendbare Methode für 'rename_' auf Objekt der Klasse "list" angewendet
Zusätzlich: Warnmeldung:
In xml_find_all.xml_node(x, make_selector(css, xpath)) :
  Invalid expression [1207]

编辑 1:

我已经清理了我的代码:

# save tables
for(i in 1:50){
  tbl_test_ <- 
    url %>% 
    xml2::read_html() %>%
    rvest::html_nodes(xpath=paste0(xpath_part1, i, xpath_part2)) %>%
    rvest::html_table(fill = TRUE) %>%
    dplyr::rename(Gesundheitsamt=1)
}

并得到错误信息:

Fehler in open.connection(x, "rb") : HTTP error 404.

关于如何更正此错误并获取 table 的任何建议?

编辑 2: 目前 table 我可以添加一些额外的信息吗?我得到这个:

# store web url
url <- "https://corona.thueringen.de/bulletin"
#Read the page
page <- url %>%  read_html()

fxp <- "/html/body/main/div[2]/div/section[1]/div[2]/div[1]/div[2]/div"

tbl_current <- 
  page %>% 
  html_nodes("table[align=left]") %>% 
  html_table() %>% 
  .[[1]] %>%
  dplyr::rename(Gesundheitsamt=1) %>% 
  dplyr::mutate(note_1=rvest::html_text(rvest::html_nodes(th_bulletin, xpath=paste0(fxp, "/div[1]/h2")))) %>% 
  dplyr::mutate(note_2=rvest::html_text(rvest::html_nodes(th_bulletin, xpath=paste0(fxp, "/div[3]/h3"))))

使用完整的 xpath 不是最好的方法,但我不知道只能从站点的特定部分获取节点 (/html/body/main/div[2]/div/section1).

对于存档的 tables,我也在寻找这些信息。我首先尝试将其存储在列表中。但这似乎只对一个 html_node 有效。

tbl_all <-
page %>%
  rvest::html_nodes("table[align=left]")

这里我得到了一个列表,每个元素都有一个 table

[[1]] table
[[2]] table
.
.
.

无法使用多个节点。

tbl_all_ <-
  page %>%
  rvest::html_nodes("table[align=left]", "h2", "h3")

有可能在列表中获得不止一种节点吗?我要得到

[[1]] for /html/body/main/div[2]/div/section[2]/ul/li[1]
table 1
table 2
h2
h3
[[2]] for /html/body/main/div[2]/div/section[2]/ul/li[2]
table 1
table 2
h2
h3
[[3]] for /html/body/main/div[2]/div/section[2]/ul/li[3] ...

提前致谢。

这里有一个更简单的方法来检索请求的 tables。
与其单独检索每个 table,不如读取页面一次(速度更快,网络流量更少),然后使用 html_nodes 和 CSS [=] 解析所有 table 23=] 选择器.
这将 return 页面上的所有 142 table。看起来 table 以 3 个为一组。通过使用 CSS 选择器指定所需的属性,可以减少此列表以提供所需的子集。

require(dplyr)
require(rvest)

# store web url
url <- "https://corona.thueringen.de/bulletin"
#Read the page
page <- url %>%  read_html() 
#retrieve all of the tables
tables<- page %>% rvest::html_nodes("table")

#cycle through the selected tables
#and stored as needed
for (table in tables) {
  print(table %>% html_table())
  Sys.sleep(2)
}

#another possible selection method:
# select tables where the align attribute = "left"
page %>% html_nodes("table[align=left]") %>% html_table()

更新
由于您正在寻找存储为列表中的列表的所有 table。这是一个修订。该页面被分解为一系列带有 class="th-lst-itm card" 的 li。第一步是检索这个父节点列表,然后解析每个父节点的所有 table。

# store web url
url <- "https://corona.thueringen.de/bulletin"
#Read the page
page <- url %>%  read_html() 

#find archived list items with class=th-lst-itm
listofitems<-page %>% rvest::html_nodes("li.th-lst-itm")

#store all of the tables in as list within a list
answer<-lapply(listofitems, function(item){
  item %>% html_nodes("table") %>% html_table()
})

# #first try but did not work for all elements
# names(answer)<-listofitems %>% html_node("h2") %>% html_text() %>% trimws()

#name the elements
names(answer)<-listofitems %>% html_node("span.th-lbl") %>% html_text() %>% trimws() 

#Pull a sample
answer[["COVID-19 / Bulletin der Thüringer Landesregierung 02/2020" ]]