I want to scrape data from this link. I used rvest and a loop to get the tables.
This is my code:
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)
}
I get the following error:
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]
edit 1:
I have cleaned my code:
# 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)
}
and get the error message:
Fehler in open.connection(x, "rb") : HTTP error 404.
Any suggestions on how to get correct for this error and get the tables?
Edit 2: At the current table can I add some additional informations. I get this with:
# 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"))))
To work with the full xpath is not the best way, but I didnt know to get the nodes only from a specific part from the site (/html/body/main/div[2]/div/section1).
For the archived tables I'm looking also for this informations. I tryed to store it at first in a list. But this seems to works only for one html_node.
tbl_all <-
page %>%
rvest::html_nodes("table[align=left]")
Here I get a list, with one element for each table
[[1]] table
[[2]] table
.
.
.
To use more than one node is not working.
tbl_all_ <-
page %>%
rvest::html_nodes("table[align=left]", "h2", "h3")
It is possible to get more then one kind of node in a list? I want to get
[[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] ...
Thanks in advance.