如何在循环中跳过假期(会产生错误)?
How to skip a holiday (which generates an error) in a loop?
我编写了一年中每一天的代码,并将其保存在单独的 .xlms 文件中。
start <- as.Date("25-01-19",format="%d-%m-%y")
end <- as.Date("17-12-19",format="%d-%m-%y")
theDate <- start
while (theDate <= end)
{
url <- (paste0("http://www.b3.com.br/pt_br/produtos-e-servicos/emprestimo-de-ativos/renda-variavel/emprestimos-registrados/renda-variavel-8AE490CA64CD50310164D1EFD6412F1C.htm?data=",format(theDate,"%d/%m/%y"),"&f=0"))
site <- read_html(url)
Info_Ajuste_HTML <- html_nodes(site,'table')
Info_ajuste <- html_text(Info_Ajuste_HTML)
head(Info_ajuste,20)
t <- head(Info_Ajuste_HTML)
lista_tabela <- site %>%
html_nodes("table") %>%
html_table(fill = TRUE)
str(lista_tabela)
head(lista_tabela[[1]], 10)
if (t =="character(0)") {
theDate <- theDate + 1
} else {
... code ...
访问的 url 是动态的,并且每天都在变化。问题是在网站下线的那几天,执行命令时产生错误"character (0)":>head(Info_ajuste, 20),错误:“{xml_nodeset(0 )}" 当执行 >head (Info_Ajust_HTML) 时。
这是因为它下载了一个 table,而在某些日子里,该站点不提供 table。
我需要创建一个 "if" 来跳过出现此错误的日子。
您可以检查 Info_Ajuste_HTML
的长度并仅在其中捕获了某些值时才执行剩余的代码。
library(rvest)
while (theDate <= end)
{
url <- (paste0("http://www.b3.com.br/pt_br/produtos-e-servicos/emprestimo-de-ativos/renda-variavel/emprestimos-registrados/renda-variavel-8AE490CA64CD50310164D1EFD6412F1C.htm?data=",format(theDate,"%d/%m/%y"),"&f=0"))
site <- read_html(url)
Info_Ajuste_HTML <- html_nodes(site,'table')
if (length(Info_Ajuste_HTML) > 0) { ### <- Added a check here
Info_ajuste <- html_text(Info_Ajuste_HTML)
head(Info_ajuste,20)
t <- head(Info_Ajuste_HTML)
##rest of the code
##rest of the code
}
}
我编写了一年中每一天的代码,并将其保存在单独的 .xlms 文件中。
start <- as.Date("25-01-19",format="%d-%m-%y")
end <- as.Date("17-12-19",format="%d-%m-%y")
theDate <- start
while (theDate <= end)
{
url <- (paste0("http://www.b3.com.br/pt_br/produtos-e-servicos/emprestimo-de-ativos/renda-variavel/emprestimos-registrados/renda-variavel-8AE490CA64CD50310164D1EFD6412F1C.htm?data=",format(theDate,"%d/%m/%y"),"&f=0"))
site <- read_html(url)
Info_Ajuste_HTML <- html_nodes(site,'table')
Info_ajuste <- html_text(Info_Ajuste_HTML)
head(Info_ajuste,20)
t <- head(Info_Ajuste_HTML)
lista_tabela <- site %>%
html_nodes("table") %>%
html_table(fill = TRUE)
str(lista_tabela)
head(lista_tabela[[1]], 10)
if (t =="character(0)") {
theDate <- theDate + 1
} else {
... code ...
访问的 url 是动态的,并且每天都在变化。问题是在网站下线的那几天,执行命令时产生错误"character (0)":>head(Info_ajuste, 20),错误:“{xml_nodeset(0 )}" 当执行 >head (Info_Ajust_HTML) 时。
这是因为它下载了一个 table,而在某些日子里,该站点不提供 table。
我需要创建一个 "if" 来跳过出现此错误的日子。
您可以检查 Info_Ajuste_HTML
的长度并仅在其中捕获了某些值时才执行剩余的代码。
library(rvest)
while (theDate <= end)
{
url <- (paste0("http://www.b3.com.br/pt_br/produtos-e-servicos/emprestimo-de-ativos/renda-variavel/emprestimos-registrados/renda-variavel-8AE490CA64CD50310164D1EFD6412F1C.htm?data=",format(theDate,"%d/%m/%y"),"&f=0"))
site <- read_html(url)
Info_Ajuste_HTML <- html_nodes(site,'table')
if (length(Info_Ajuste_HTML) > 0) { ### <- Added a check here
Info_ajuste <- html_text(Info_Ajuste_HTML)
head(Info_ajuste,20)
t <- head(Info_Ajuste_HTML)
##rest of the code
##rest of the code
}
}